C# – A read-only CheckBox in C# WPF

bindingccheckboxwpf

I am having a tricky problem, I want some slightly unusual behaviour from a checkbox and can't seem to figure it out. Any suggestions would be most welcome. The behaviour I want is:

  1. The CheckBox is enabled and ready for the user to click, IsChecked represents a bound boolean value stored in a data structure
  2. The user clicks the CheckBox causing the click event to fire but the bound value in the data structure is NOT updated and the visual representation of the CheckBox is NOT updated but it is disabled to stop further clicking
  3. The click event triggers a message to be sent to a remote device which takes some time to respond
  4. The remote device responds causing the data structure to be updated with the new value, the binding then updates the isChecked status and the CheckBox gets reenabled for further clicking

The problem I have is that although a OneWay data binding works at not updating the data structure when the CheckBox is clicked, the visual representation does change (which I think is odd, shouldn't IsChecked now act like a pointer to the value in the data structure).

I can reverse the change in the Click() event and do the disable there as well but this is pretty messy. I can also have the set property of the data structure value to set an isEnabled value which is also bound to reenable the CheckBox but that seems messy too.

Is there a clean way to do this? Perhaps with a derived CheckBox class? How can I stop the visual representation getting updated?

Thanks

Ed

Best Answer

What about data binding to the IsHitTestVisible property?

For example, assuming an MVVM approach:

  1. Add a IsReadOnly property to your view model, initially set as true to allow click.
  2. Binding this property to CheckBox.IsHitTestVisible.
  3. After the first click, update your view model to set this value to false, preventing any further clicks.

I don't have this exact requirement, I just needed an always read only checkbox, and it seems to solve the problem nicely. Also note Goran's comment below about the Focusable property.