Value Converter In WPF

WPF provides with binding using which we can bind two elements to each other. For example, a textbox is been bind to some other textbox, when you change the text of any of the two textboxes it will reflect on the other textbox too(“The default mode is Two-way”).

Binding

This case is for the similar attributes where the object formats is similar (Text for the above example), but when you need to bind two different elements like a textBox and a checkBox where the textBox accepts text while checkBox accepts a boolean value whether isChecked true/false. For this case we require to convert the text to boolean type, and that's where we need a Value Converter.

Value convertor

A Value Converter is a class where we can write the required conversion logic. The value converter class should implement an interface named “IValueConverter” which provides two methods:

  1. Convert(object value)

    The convert method will convert the value from Target to Source.

  2. ConvertBack (object value)

    The convert method will convert the value from Source to Target.

Let us take an example where we have a textbox and a checkbox, when the user types “Check” in the textbox the checkbox gets checked and when he types “Uncheck” the checkbox gets unchecked, and vice versa.

{ Creating a Value Converter: }

Step 1: In your WPF Project, create a value converter class:

Class

Step 2: Implement the IValueConverter interface in the created class:

For IValueConverter we need the System.Windows.Data namespace.

IValueConverter

Step 3: Implement the two methods Convert and ConvertBack:

Implement the two methods

Step 4: Write the logic for convert method and the convert back method:

 logic for convert method

XAML part: Now in the xaml section we need to add a textbox and a checkbox and bind the checkbox to the textbox and specify the converter we want to use.

Step 5: Adding the textbox and the checkbox.

  1. <Window x:Class="valueConverterDemo.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  6.         xmlns:local="clr-namespace:valueConverterDemo"  
  7.         mc:Ignorable="d"  
  8.         Title="MainWindow" Height="350" Width="525">  
  9.      <Grid>  
  10.             <TextBox Name="txt1" Margin="171,35,170,250" ></TextBox>  
  11.             <CheckBox Name="chkbox1" Margin="194,97,184,195"></CheckBox>  
  12.     </Grid>  
  13. </Window>  
Step 6: For binding we need to map the namespace of your converter to the XAML namespace. Later you can create an instance of the value converter and then you can refer it by using {Static Resource}.

 

  1. <Window x:Class="valueConverterDemo.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  6.         xmlns:local="clr-namespace:valueConverterDemo"  
  7.         mc:Ignorable="d"  
  8.         Title="MainWindow" Height="350" Width="525">  
  9.     <Window.Resources>  
  10.         <local:TextToCheckboxValueConverter x:Key="myConverter" 
  11.     </Window.Resources>  
  12.         <Grid>  
  13.         
  14.             <TextBox Name="txt1" Margin="171,35,170,250" ></TextBox>  
  15.             <CheckBox Name="chkbox1" Margin="194,97,184,195"  
  16.                 IsChecked="{Binding Text,   
  17.                 Converter={StaticResource myConverter},   
  18.                 ElementName=txt1}" ></CheckBox>  
  19.     </Grid>  
  20. </Window>  
In the above step we have created the instance in the Windows.Resource tag with the key as “myConverter”. This can be referred in the checkbox using the StaticResource as myConverter and the element name as the name of the control to bind.

Output:

Output

Run

Check

 

Main Window

Up Next
    Ebook Download
    View all
    Learn
    View all