Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - Is it possible to negate the result of a data binding expression?

I know this works fine:

<TextBox IsEnabled="{Binding ElementName=myRadioButton, Path=IsChecked}" />

...but what I really want to do is negate the result of the binding expression similar to below (psuedocode). Is this possible?

<TextBox IsEnabled="!{Binding ElementName=myRadioButton, Path=IsChecked}" />
like image 915
Taylor Leese Avatar asked Dec 04 '09 00:12

Taylor Leese


People also ask

What is oneway binding WPF?

In One Way binding, source control updates the target control, which means if you change the value of the source control, it will update the value of the target control. So, in our example, if we change the value of the slider control, it will update the textbox value, as shown below.

What is DataContext in WPF?

The DataContext property is the default source of your bindings, unless you specifically declare another source, like we did in the previous chapter with the ElementName property. It's defined on the FrameworkElement class, which most UI controls, including the WPF Window, inherits from.

What is DataTemplate WPF?

DataTemplate is about the presentation of data and is one of the many features provided by the WPF styling and templating model. For an introduction of the WPF styling and templating model, such as how to use a Style to set properties on controls, see the Styling and Templating topic.


1 Answers

You can do this using an IValueConverter:

public class NegatingConverter : IValueConverter
{
  public object Convert(object value, ...)
  {
    return !((bool)value);
  }
}

and use one of these as the Converter of your Binding.

like image 117
itowlson Avatar answered Sep 28 '22 08:09

itowlson