WPF Databinding the negative of a boolean RRS feed

  • Question

  • Hi:

    My apologies if I posted to the wrong forum, could not find a WPF forum.

    I'm finally looking at WPF and find that the learning curve is much steeper than I anticipated coming from windows forms.  In any case  wpf window with two radio buttons (radio1 and radio2) on it and two text boxes (tb1 and tb2).  The XAML to enable the text boxes based on the radio selection is straightforward:

    <TextBox Name="tb1" IsEnabled="{Binding ElementName=radio1, Path=IsChecked}" />

    How do I do the negative?  I would like to disable tb1 when radio1 is checked.


    • Moved by Helen Zhou Thursday, May 6, 2010 1:48 AM (From:Windows Forms Data Controls and Databinding)
    Wednesday, May 5, 2010 5:14 PM


All replies

  • You can use DataTrigger as shown in following link


    Gaurav Khanna
    • Marked as answer by Robotuner Thursday, May 6, 2010 2:34 AM
    Wednesday, May 5, 2010 5:41 PM
  • You can also use an IValueConverter:

    public class BoolInverter : IValueConverter
      #region IValueConverter Members
      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        return !(bool)value;
      public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        throw new NotImplementedException();

    Then use it in your XAML like this:

      <converters:BoolInverter x:Key="BoolInverter"/>
    <TextBox Name="tb1" IsEnabled="{Binding ElementName=radio1, Path=IsChecked, Converter={StaticResource BoolInverter}}" />
    Hope this helps.
    Thursday, May 6, 2010 2:27 AM
  • Thanks Michael;

    I tried the your IValueConverter solution and it does work.  My impression is that it is overkill.  The DataTrigger solution appears more elegant to me, I also like the fact that it is entirely in XAML.

    Thursday, May 6, 2010 2:37 AM