DescriptionThis sample demonstrates how to create declarative value converters for WPF data binding using XAML.
Note In order to use LambdaConverter.cs class (see Downloads) you have to add Dynamic.cs file to your project.
LambdaConverter class lets you create converters for WPF’s data-binding dynamically in XAML.
It saves you the pain of having to create a new class implementing IValueConverter each time you need to slightly change the value you bind to.
Example 1: This XAML fragment will enable the button only if the user typed text containing the character ‘7’ in the text box.
<Window x:Class="WpfTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:XamlConverter="clr-namespace:XamlConverter;assembly=XamlConverter"
xmlns:System="clr-namespace:System;assembly=mscorlib"
Title="XAML Converter Demo" Height="207" Width="385">
<Window.Resources>
<XamlConverter:LambdaConverter x:Key="contains">value.Contains(parameter)</XamlConverter:LambdaConverter>
</Window.Resources>
<StackPanel >
<GroupBox Margin="0,0,0,20" Header="Text has to include the character '7'">
<StackPanel>
<TextBox Name="txtValue" />
<Button IsEnabled="{Binding Path=Text, ElementName=txtValue, Converter={StaticResource contains}, ConverterParameter='7' }">Click Me</Button>
</StackPanel>
</GroupBox>
</StackPanel>
</Window>
Example 2: Here width of the button will be half the value of the slider.
<Window x:Class="WpfTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:XamlConverter="clr-namespace:XamlConverter;assembly=XamlConverter"
xmlns:System="clr-namespace:System;assembly=mscorlib"
Title="XAML Converter Demo" Height="207" Width="385">
<Window.Resources>
<XamlConverter:LambdaConverter x:Key="half">value/2.0</XamlConverter:LambdaConverter>
</Window.Resources>
<StackPanel >
<GroupBox Margin="0,0,0,20" Header="Dynamically adjusted width (type come text to grow the button)">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Slider Name="width" Minimum="10" Maximum="300" Value="100" />
<Button Grid.Row="1" HorizontalAlignment="Center"
Width="{Binding Path=Value, ElementName=width, Converter={StaticResource half}}">Click Me</Button>
</Grid>
</GroupBox> </StackPanel>
</Window>
Example 3: Two-way binding and two-way value conversion.
<Window x:Class="WpfTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:XamlConverter="clr-namespace:XamlConverter;assembly=XamlConverter"
Title="XAML Converter Demo" Height="281" Width="385">
<Window.Resources>
<XamlConverter:LambdaConverter x:Key="checked">
<XamlConverter:LambdaConverter.Lambda>string.Compare(value, "yes", true) == 0</XamlConverter:LambdaConverter.Lambda>
<XamlConverter:LambdaConverter.BackLambda>value ? "yes" : "no"</XamlConverter:LambdaConverter.BackLambda>
</XamlConverter:LambdaConverter>
</Window.Resources>
<StackPanel >
<GroupBox Margin="0,0,0,20" Header="Typing 'yes' will mark the checkbox (two way binding)">
<StackPanel>
<TextBox Name="txtChecked" />
<CheckBox IsChecked="{Binding Path=Text, ElementName=txtChecked, Converter={StaticResource checked}, Mode=TwoWay }">Click Me</CheckBox>
</StackPanel>
</GroupBox>
</StackPanel>
</Window>
LambdaConverter contains a cache of compiled lambdas and will not parse/compile the same lambda twice.
While parsing code at runtime may not be suitable for every project, LambdaConverter allows rapid implementation of complex databinding scenarios.