Boolean converter

edit

BooleanToValueConverter converts a boolean to the value of your choice: declare it as a resource with the values for true and false, then use it in bindings.

Show or hide a control

<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:wpf="clr-namespace:Meziantou.Framework.WPF;assembly=Meziantou.Framework.WPF">
    <UserControl.Resources>
        <wpf:BooleanToValueConverter x:Key="BoolToVisibility" TrueValue="{x:Static Visibility.Visible}" FalseValue="{x:Static Visibility.Collapsed}" />
    </UserControl.Resources>
    <TextBlock Text="Shown when IsEnabled is true" Visibility="{Binding IsEnabled, Converter={StaticResource BoolToVisibility}}" />
</UserControl>

Declare one converter for each pair of values. The values can be of any type: a Visibility, a text, a brush, a number…

Handle null values

NullValue is returned when the value is null, for instance a bool? property that is not set yet:

<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:wpf="clr-namespace:Meziantou.Framework.WPF;assembly=Meziantou.Framework.WPF">
    <UserControl.Resources>
        <!-- NullValue is used when the value is null or cannot be converted to a boolean -->
        <wpf:BooleanToValueConverter x:Key="ApprovalToText" TrueValue="Approved" FalseValue="Rejected" NullValue="Pending" />
    </UserControl.Resources>
    <TextBlock Text="{Binding IsApproved, Converter={StaticResource ApprovalToText}}" />
</UserControl>

Conversion rules

ValueResult
trueTrueValue
falseFalseValue
nullNullValue, or FalseValue when NullValue is not set
Another value that converts to a boolean: a number (0 is false, any other number is true), or the text "true" or "false"TrueValue or FalseValue
A value that does not convert to a boolean, such as "abc" or a dateNullValue, or FalseValue when NullValue is not set

The converter is one-way: ConvertBack throws a NotSupportedException. Do not use it in a TwoWay binding.