Tuesday, July 26, 2011

Multi Value Converters in WPF



Multiple Bindings
Multiple binding enables target to bind with multiple sources. It aggregate multiple bindings together and out single value for target. Multibinding requires converter class which is responsible to combine all different types and values from different binding class and return single type and value for target. The converter class must implement IMultiValueConverter interface. This interface quite similar to IValueConverter and contains two methods Convert and ConvertBack. Convert Method contains array of values as parameter and ConvertBack method contains array of types as parameter.

<Window.Resources>
    <local:LableVisibilityMultiValueConverter
        x:Key="LabelMultiValueConverter" />
</Window.Resources>
<StackPanel>
    <CheckBox Content="Show Name"
                x:Name="ShowNameCheckbox" Margin="5" />
    <StackPanel Orientation="Horizontal" Margin="5">
        <TextBlock Text="Enter Name: "
                    VerticalAlignment="Center" />
        <TextBox x:Name="NameTextBox"
                    Width="150"/>
    </StackPanel>
    <StackPanel Margin="5" Orientation="Horizontal">
        <TextBlock Text="Your Name: "
                    VerticalAlignment="Center" />
        <Label Content="{Binding ElementName=NameTextBox,
               Path=Text}">
            <Label.Visibility>
                <MultiBinding
                    Converter="{StaticResource LabelMultiValueConverter}">
                    <Binding ElementName="ShowNameCheckbox"
                             Path="IsChecked" />
                    <Binding ElementName="NameTextBox"
                             Path="Text" />
                </MultiBinding>
            </Label.Visibility>
        </Label>
    </StackPanel>
</StackPanel>

public class LableVisibilityMultiValueConverter :IMultiValueConverter
{

    public object Convert(object[] values, Type targetType,
        object parameter, System.Globalization.CultureInfo culture)
    {
        bool showCheckbox = (bool)values[0];
        string text = values[1].ToString();
        if (text.Length > 0 && showCheckbox)
            return Visibility.Visible;
        else
            return Visibility.Collapsed;
    }

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





Label’s Visibility property is bound with two sources one is Checkbox’s Checked property and another is Textbox’s Text property. If checkbox is checked and Textbox’s text length is greater than zero then only Label is visible. To achieve this I need to write one Multivalue converter. In converter method i am getting Checkbox’s checked status as well getting textbox’s text as an object array. Now i need to do some processing on values from array and after that need to  return single value for target object. In this example target object is Label and target property is Visibility so returned Visibility enum.

Converter Parameters
Converter Parameter can also be passed with Multibinding Converter similar to normal ValueConverter. Multibinding object consists of many binding and each binding contains all the properties which normal binding has. It can also take converter and converter parameters with it.

<Label Content="{Binding ElementName=NameTextBox,
        Path=Text}">
    <Label.Visibility>
        <MultiBinding
            Converter="{StaticResource LabelMultiValueConverter}"
            ConverterParameter="Test">
            <Binding ElementName="ShowNameCheckbox"
                        Path="IsChecked"
                        Converter="{StaticResource TestConverter}"
                        ConverterParameter="1"/>
            <Binding ElementName="NameTextBox"
                        Path="Text" />
        </MultiBinding>
    </Label.Visibility>
</Label>

1 comment:

  1. how to visibility collapsed the header of MULTIPLE datagrid column based on radio button selection.

    ReplyDelete