The BooleanConverter class provides a way to convert a value to a boolean value. The converter returns true when the specified parameter is the string representation of the value. This is useful with data binding to place a checkmark in menus or to check or uncheck a toggle button. The class can be used in XAML as a static resource to automatically convert bound property values. Supported types are: System.Enum, int, int?, bool?, char and char?.

Syntax

[ValueConversion(typeof(int), typeof(bool))]
[ValueConversion(typeof(int?), typeof(bool))]
[ValueConversion(typeof(System.Enum), typeof(bool))]
[ValueConversion(typeof(bool?), typeof(bool))]
[ValueConversion(typeof(char), typeof(bool))]
[ValueConversion(typeof(char?), typeof(bool))]
public class BooleanConverter : IValueConverter
Public Class BooleanConverter
  Implements IValueConverter

Introduced: 16.0.

Examples

The following XAML example shows how to bind radio buttons to the InputFormat.NumberedListFormat property using the BooleanConverter class and the InputFormat.NumberFormats property.

?
xmlns:tx="clr-namespace:TXTextControl.WPF;assembly=TXTextControl.WPF"
?
<Window.Resources>
    <tx:BooleanConverter x:Key="boolConv" />
</Window.Resources>
?
<RadioButton
    Content="{Binding ElementName=textControl1, Path=InputFormat.NumberFormats[0]}"
    GroupName="numberFormat"
    IsChecked="{Binding
        ElementName=textControl1,
        Path=InputFormat.NumberedListFormat,
        Converter={StaticResource boolConv},
        ConverterParameter=ArabicNumbers}" />
<RadioButton
    Content="{Binding ElementName=textControl1, Path=InputFormat.NumberFormats[1]}"
    GroupName="numberFormat"
    IsChecked="{Binding
        ElementName=textControl1,
        Path=InputFormat.NumberedListFormat,
        Converter={StaticResource boolConv},
        ConverterParameter=Letters}" />
?

Methods

Method Description
Convert Converts a value to a boolean.
ConvertBack Converts a boolean to a value of the type specified through the targetType parameter.