What is Value Converter in WPF?
Value converter is a class which translates/converts a value from the source type and provide output as target type and vice-versa. It is used at the time of data binding with the UI element in WPF. These classes implement the IValueConverter interface that consist of two methods, Convert() and ConvertBack().
When source updates the target object, Convert method gets called.
When target updates source object, ConvertBack method gets called.
For example - there is a scenario where the value exists in Boolean format i.e. true or false in data source but user wants to display in string format as "yes" Or "no" instead of displaying true or false. Here, ValueConverter will take input from source as true or false and will provide output to target as "yes" in case of Boolean true or "no" in case of Boolean false respectively.
Lets have a look on following example, ValueConverter.xaml
<Window x:Class="WpfApplicationSample.ValueConverter"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplicationSample.Converters"
mc:Ignorable="d"
Title="ValueConverter" Height="130" Width="300">
<Window.Resources>
<local:BooleanToYesNoConverter x:Key="BooleanToYesNoConverter"/>
</Window.Resources>
<Grid>
<StackPanel Margin="15">
<CheckBox Name="chkBox" Margin="0,0,0,5"/>
<TextBox Name="txtValue" Text="{Binding ElementName=chkBox, Path=IsChecked, Converter={StaticResource BooleanToYesNoConverter}}" />
</StackPanel>
</Grid>
</Window>
Now, add a new class in your project called BooleanToYesNoConverter.cs under the sub-folder of "Common".
using System;
using System.Windows.Data;
namespace WpfApplicationSample.Converters
{
class BooleanToYesNoConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is bool)
{
if ((bool)value == true)
return "yes";
else
return "no";
}
return "no";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
switch (value.ToString().ToLower())
{
case "yes":
return true;
case "no":
return false;
default:
return Binding.DoNothing;
}
}
}
}
The output of above mentioned example is that textbox will display the string value as "yes" or "no" on the basis of checked value type of checkbox ie either true or false .
The point to note that there is a local namespace has been added in the ValueConverter.xaml file i.e. "xmlns:local="clr-namespace:WpfApplicationSample.Converters" after that added this under the <Window.Resources> tag i.e. <local:BooleanToYesNoConverter x:Key="BooleanToYesNoConverter"/>
Now, key " BooleanToYesNoConverter" converter can be use anywhere inside this window page only to convert value from bool type to string type.
0 Comment(s)