locked
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.

    Thanks;

    • 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

Answers

All replies

  • You can use DataTrigger as shown in following link

    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/98c224af-cac0-4108-88bb-1e97d701e3a8


    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();
      }
    
      #endregion
    }

    Then use it in your XAML like this:

    <Window.Resources>
      <converters:BoolInverter x:Key="BoolInverter"/>
    </Window.Resources>
    
    <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