Monday, July 25, 2011

Value Converters in WPF




Value converters are used in Data binding. When source object type and target object type are different at that time value converts are used to manipulate data between source and target. Converter class must implement IValueConverter interface. The IValueConverter interface consists of two methods Convert and ConvertBack. Convert Method gets called when source updates target object and ConvertBack method gets called when target updates source object. 

<StackPanel>
    <CheckBox x:Name="ShowBackgroundCheckbox"
                Content="Show Background" Margin="5" />
    <TextBox Text="Value Converter" Margin="5"
                Background="{Binding
        ElementName=ShowBackgroundCheckbox, Path=IsChecked}"/>
</StackPanel> 

In above example Textbox’s Background property is bound with Checkbox’s IsChecked property. This binding won't work because IsChecked property is Boolean and background is Brush type. So for successful data binding the source and target type must match which is not true in above XAML code. So we need to create one value converter which manipulates checkbox’s IsChecked property and textbox’s Background property.

<Window.Resources>
    <local:BooleanToBackgroundConverter x:Key="booleanToBackground" />
</Window.Resources>

<StackPanel>
    <CheckBox x:Name="ShowBackgroundCheckbox"
              Content="Show Background" Margin="5" />
    <TextBox Text="Value Converter" Margin="5"
        Background="{Binding
        ElementName=ShowBackgroundCheckbox, Path=IsChecked,
        Converter={StaticResource booleanToBackground}}"/>
</StackPanel>

public class BooleanToBackgroundConverter:IValueConverter
{
    public object Convert(object value, Type targetType,
        object parameter, System.Globalization.CultureInfo culture)
    {
        bool result = (bool)value;
        if (result)
        {
            return Brushes.LightBlue;
        }
        return null;
    }

    public object ConvertBack(object value, Type targetType,
        object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
}












When Show Background checkbox is checked the textbox get filled with LightBlue background. To achieve that I created one class named BooleanToBackgroundConverter which implements IValueConverter interface. In our example Checkbox is source for textbox. So whenever user changes checkbox’s value the Convert method will get called. The ConvertBack method converts target value to source value. This method will get called when twoway binding established between source and target.

Converter Parameters
Additional information can be passed to value converter using converter parameter.

<TextBox Text="Value Converter" Margin="5"
    Background="{Binding
    ElementName=ShowBackgroundCheckbox, Path=IsChecked,
    Converter={StaticResource booleanToBackground}, ConverterParameter=1}"/>

public object Convert(object value, Type targetType,
    object parameter, System.Globalization.CultureInfo culture)
{
    bool result = (bool)value;
    int param = Int32.Parse(parameter.ToString());
    if (result)
    {
        if (param == 1)
            return Brushes.Red;
        return Brushes.LightBlue;
    }
    return null;
}



No comments:

Post a Comment