Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: Display a bool value as "Yes" / "No"

I have a bool value that I need to display as "Yes" or "No" in a TextBlock. I am trying to do this with a StringFormat, but my StringFormat is ignored and the TextBlock displays "True" or "False".

<TextBlock Text="{Binding Path=MyBoolValue, StringFormat='{}{0:Yes;;No}'}" /> 

Is there something wrong with my syntax, or is this type of StringFormat not supported?

I know I can use a ValueConverter to accomplish this, but the StringFormat solution seems more elegant (if it worked).

like image 871
John Myczek Avatar asked May 08 '09 21:05

John Myczek


2 Answers

You can also use this great value converter

Then you declare in XAML something like this:

<local:BoolToStringConverter x:Key="BooleanToStringConverter" FalseValue="No" TrueValue="Yes" /> 

And you can use it like this:

<TextBlock Text="{Binding Path=MyBoolValue, Converter={StaticResource BooleanToStringConverter}}" /> 
like image 179
alf Avatar answered Oct 19 '22 03:10

alf


Your solution with StringFormat can't work, because it's not a valid format string.

I wrote a markup extension that would do what you want. You can use it like that :

<TextBlock Text="{my:SwitchBinding MyBoolValue, Yes, No}" /> 

Here the code for the markup extension :

public class SwitchBindingExtension : Binding {     public SwitchBindingExtension()     {         Initialize();     }      public SwitchBindingExtension(string path)         : base(path)     {         Initialize();     }      public SwitchBindingExtension(string path, object valueIfTrue, object valueIfFalse)         : base(path)     {         Initialize();         this.ValueIfTrue = valueIfTrue;         this.ValueIfFalse = valueIfFalse;     }      private void Initialize()     {         this.ValueIfTrue = Binding.DoNothing;         this.ValueIfFalse = Binding.DoNothing;         this.Converter = new SwitchConverter(this);     }      [ConstructorArgument("valueIfTrue")]     public object ValueIfTrue { get; set; }      [ConstructorArgument("valueIfFalse")]     public object ValueIfFalse { get; set; }      private class SwitchConverter : IValueConverter     {         public SwitchConverter(SwitchBindingExtension switchExtension)         {             _switch = switchExtension;         }          private SwitchBindingExtension _switch;          #region IValueConverter Members          public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)         {             try             {                 bool b = System.Convert.ToBoolean(value);                 return b ? _switch.ValueIfTrue : _switch.ValueIfFalse;             }             catch             {                 return DependencyProperty.UnsetValue;             }         }          public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)         {             return Binding.DoNothing;         }          #endregion     }  } 
like image 21
Thomas Levesque Avatar answered Oct 19 '22 01:10

Thomas Levesque