locked
Using "Not" for a Dependencyobject Binding? RRS feed

  • Question

  • I have a situation where if a checkbox is checked, then a series of comboboxes should be disabled. So, if IsChecked="true" on the checkbox, then IsEnabled="false" needs to be implemented on the comboboxes.

    Thus far, I have been able to set up this entire data entry form using a flavor of MVVM (no frameworks involved, just hiding all of the WCF event-handling code and so on within a ViewModel class) and data binding is used exclusively in the XAML.

    I wanted to use DependencyObject Binding to solve this problem. Ironically, if the situation was reversed - that if the checkbox was checked then the comboboxes would be enabled I could just use:

    IsEnabled="{Binding ElementName=chkDynamic, Path=IsChecked}" (where chkDynamic is the checkbox and the IsEnabled property is in the XAML for the combobox)

    But, what I really need is:

    IsEnabled="{Binding ElementName=chkDynamic, Path=!IsChecked}" (applying the NOT operator to the IsChecked path). 

    So, the ultimate question becomes...Can I do this with DependencyObjectBinding or do I have to write a value converter to accomplish the task.

    I'm thinking that a value converter is the only way to do this. Any ideas out there?

    Wednesday, May 12, 2010 12:04 PM

Answers

  • yes, a value converter is usually the standard way to go.  typically this is called an InverseBooleanConverter.

    you could also create a one-off IsNotChecked property in the class that your DataContext is bound to, but the inverse converter is more readable and much more reusable.

    Wednesday, May 12, 2010 12:46 PM

All replies

  • yes, a value converter is usually the standard way to go.  typically this is called an InverseBooleanConverter.

    you could also create a one-off IsNotChecked property in the class that your DataContext is bound to, but the inverse converter is more readable and much more reusable.

    Wednesday, May 12, 2010 12:46 PM
  • Ok. Thanks.

    Thursday, May 13, 2010 8:09 AM