Udostępnij za pośrednictwem


Array as a WPF ConverterParameter

A WPF value converter accepts a value and an optional parameter. The parameter can be specified in the XAML binding as a string like the following:

 

<TextBlock Background=”{Binding Path=myElement, Converter={StatisResource MyConverter}, ConverterParameter=myParameterText}” />

 

Instead of one string, what if you want to pass an array of strings as a converter parameter? What about an array of brushes?

The trick is to specify the binding in its own XAML tag and use Binding.ConverterParameter and Array. Here is an example of how to pass an array of Strings and an array of Brushes into a value converter. Note the uses of Binding, Binding.ConverterParameter, and Array.

 

<Window x:Class="ConverterParameters.Window1"
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mscor="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:ConverterParameters"
WindowStartupLocation="CenterScreen"
Title="Array as ConverterParameter" Height="100" Width="300">
<Window.Resources>
<local:IntToStringMux x:Key="IntToStringMux"/>
<local:IntToBrushMux x:Key="IntToBrushMux"/>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

        <TextBlock Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Center" TextAlignment="Center">
<TextBlock.Text>
<Binding ElementName="mySelector" Path="SelectedIndex" Converter="{StaticResource IntToStringMux}">
<Binding.ConverterParameter>
<x:Array Type="mscor:String">
<mscor:String>A selected</mscor:String>
<mscor:String>B selected</mscor:String>
<mscor:String>C selected</mscor:String>
<mscor:String>D selected</mscor:String>
</x:Array>
</Binding.ConverterParameter>
</Binding>
</TextBlock.Text>
<TextBlock.Background>
<Binding ElementName="mySelector" Path="SelectedIndex" Converter="{StaticResource IntToBrushMux}">
<Binding.ConverterParameter>
<x:Array Type="Brush">
<SolidColorBrush Color="LawnGreen"/>
<SolidColorBrush Color="LightSkyBlue"/>
<SolidColorBrush Color="LightCoral"/>
</x:Array>
</Binding.ConverterParameter>
</Binding>
</TextBlock.Background>
</TextBlock>

        <ComboBox x:Name="mySelector" Grid.Row="1" SelectedIndex="0">
<ComboBox.Items>
<ComboBoxItem>Item A</ComboBoxItem>
<ComboBoxItem>Item B</ComboBoxItem>
<ComboBoxItem>Item C</ComboBoxItem>
<ComboBoxItem>Item D</ComboBoxItem>
</ComboBox.Items>
</ComboBox>
</Grid>
</Window>

 

 

For completeness, the implementations for the IntToStringMux and IntToBrushMux converters are also included below.

 

[ValueConversion(typeof(int), typeof(String))]
public class IntToStringMux : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
String result = String.Empty;
int itemIndex = 0;
String[] itemDescriptions = parameter as String[];

        if (value != null && value.GetType() == typeof(int))
{
itemIndex = (int)value;
}

        if (itemDescriptions != null && itemIndex < itemDescriptions.Length)
{
result = itemDescriptions[itemIndex];
}

        return result;
}

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}

[ValueConversion(typeof(int), typeof(Brush))]
public class IntToBrushMux : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Brush result = Brushes.Red;
int itemIndex = 0;
Brush[] brushes = parameter as Brush[];

        if (value != null && value.GetType() == typeof(int))
{
itemIndex = (int)value;
}

        if (brushes != null && itemIndex < brushes.Length)
{
result = brushes[itemIndex];
}

        return result;
}

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}

 

That’s it!

Hope this helps. -Tan