I’ve seen this done a few different ways, but I think this is the best way because you can reuse it. Below is the converter, notice the 2 public properties.
public class BoolToTextConverter : IValueConverter { public string TrueText { get; set; } public string FalseText { get; set; } public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return ((bool)value) ? TrueText : FalseText; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } }
Then in your XAML resource section you would set the properties:
<localHelpers:BoolToTextConverter x:Key="boolToTextConverter"> <localHelpers:BoolToTextConverter.TrueText> Sent </localHelpers:BoolToTextConverter.TrueText> <localHelpers:BoolToTextConverter.FalseText> Not Sent </localHelpers:BoolToTextConverter.FalseText> </localHelpers:BoolToTextConverter>
So this is setting the 2 public properties. The final part is to bind this to the TextBox, in this example I’m binding to a boolean property named “sent”. The result is that the text will be “Sent” if it is true and “Not Sent” if it is false.
<TextBlock Text="{Binding Path=sent, Converter={StaticResource boolToTextConverter}}" VerticalAlignment="Center" Margin="4" TextAlignment="Center" />
