Converters in WPF

Introduction 

This article explains and demonstrates a little bit about WPF converters. It explains the types of converters available in WPF and the uses of them. In WPF binding, is the great feature that helps to flow data between two WPF UI objects. The object that emits the data is called the source and the object that accepts the data is called the target.

The WPF converters acts as a bridge between the source and the target if the source and target have different data formats or need some conversion. For example, sometimes we need to convert data from one format to another format, when it flows from the source to the target or vice-versa the conversion is required.

There are two types of Value Converters in WPF, Value Converters and MultiValue Coverters.

A Value Converter is required when a target is bound with one source, For instance you have a text box and a button control. You want to enable or disable the button control when the text of text box is filled or null.
 

A MultiValue Coverter is required when your target is bound with multiple sources and the source and targets have different data formats or need some conversion.

For Example-1, let's say you have three text box controls and one button control and you want to enable the button control when all the text of the text boxes are filled.

Code Example-1

Declaration of Value Converter
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Windows.Data;  
  7.   
  8. namespace ValueConverters  
  9. {  
  10.    public class ValueConverter:IValueConverter  
  11.     {  
  12.         public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)  
  13.         {  
  14.             bool isenable = true;  
  15.             if (string.IsNullOrEmpty(value.ToString()))  
  16.             {  
  17.                isenable = false;  
  18.             }  
  19.             return isenable;  
  20.         }  
  21.   
  22.   
  23.         public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)  
  24.         {  
  25.             throw new NotImplementedException();  
  26.         }  
  27.     }  
  28. }  
Code Example-2

Declaration of Value Converter 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Windows.Data;  
  7.   
  8. namespace ValueConverters  
  9. {  
  10.     class CheckBoxCheckConverter:IValueConverter  
  11.     {  
  12.         public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)  
  13.         {  
  14.             if (value.ToString().ToUpper() == "MARRIED")  
  15.             {  
  16.                 return true;  
  17.             }  
  18.             else  
  19.             {  
  20.                 return false;  
  21.             }  
  22.         }  
  23.   
  24.         public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)  
  25.         {  
  26.             bool married = System.Convert.ToBoolean(value);  
  27.             if (married == true)  
  28.                 return "Married";  
  29.             else  
  30.                 return "Unmarried";  
  31.         }  
  32.     }  
  33. }  
Code Example-3
Declaration of Multi Value Converter 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Windows.Data;  
  7.   
  8. namespace ValueConverters  
  9. {  
  10.    public class MultivalueConverter:IMultiValueConverter  
  11.     {  
  12.         public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)  
  13.         {  
  14.             if (values.Count() >= 2)  
  15.             {  
  16.                 if (string.IsNullOrEmpty(values[0].ToString()) || string.IsNullOrEmpty(values[1].ToString()))  
  17.                     return false;  
  18.                 else return true;  
  19.             }  
  20.             else  
  21.                 return false;  
  22.         }  
  23.   
  24.         public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)  
  25.         {  
  26.             throw new NotImplementedException();  
  27.         }  
  28.     }  
  29. }  
Code Example-4

Declaration of Multi Value Converter
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Windows.Data;  
  7.   
  8. namespace ValueConverters  
  9. {  
  10.     public class FullNameConverter : IMultiValueConverter  
  11.     {  
  12.         public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)  
  13.         {  
  14.             if (values != null)  
  15.             {  
  16.                 return values[0] + " " + values[1];  
  17.             }  
  18.             return " ";  
  19.         }  
  20.   
  21.         public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)  
  22.         {  
  23.             string[] values=null;  
  24.             if (value != null)  
  25.                 return values = value.ToString().Split(' ');  
  26.             return values;  
  27.         }  
  28.     }  
  29. }  
Code Example-5 

Implimentation of Value Converter 
  1. <Window x:Class="ValueConverters.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         xmlns:local="clr-namespace:ValueConverters"  
  5.         Title="MainWindow" Height="350" Width="525">  
  6.     <Window.Resources>  
  7.         <local:ValueConverter x:Key="valueconverter"></local:ValueConverter>  
  8.         <local:CheckBoxCheckConverter x:Key="checkBoxCheckConverter"></local:CheckBoxCheckConverter>  
  9.     </Window.Resources>  
  10.     <Grid>  
  11.         <TextBlock Text="Value Converter Exampkle" HorizontalAlignment="Stretch" VerticalAlignment="Top" TextAlignment="Center"></TextBlock>  
  12.         <TextBox Name="txtFirstName" HorizontalAlignment="Left" VerticalAlignment="Top" Height="36" Width="255" Margin="136,38,0,0" ></TextBox>  
  13.         <Button Content="Click" HorizontalAlignment="Left" VerticalAlignment="Top" Height="23" Width="50" Margin="230,101,0,0" IsEnabled="{Binding Path=Text, ElementName=txtFirstName,Converter={StaticResource valueconverter}}"></Button>  
  14.         <CheckBox Content="Married" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="307,108,0,0" IsChecked="{Binding Path=Text, ElementName=txtFirstName,Converter={StaticResource checkBoxCheckConverter}}"></CheckBox>  
  15.         <TextBlock Text="MultiValue Converter Exampkle" HorizontalAlignment="Stretch" VerticalAlignment="Top" TextAlignment="Center" Margin="10,146,-10,0"></TextBlock>  
  16.     </Grid>  
  17. </Window>  
Code Example-6

Implimentation of Multi Value Converter
  1. <Window x:Class="ValueConverters.MultiValueConverter"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         xmlns:local="clr-namespace:ValueConverters"  
  5.         Title="MultiValueConverter" Height="300" Width="448.872">  
  6.     <Window.Resources>  
  7.         <local:MultivalueConverter x:Key="multivalueConverter"></local:MultivalueConverter>  
  8.         <local:FullNameConverter x:Key="fullNameConverter"></local:FullNameConverter>  
  9.     </Window.Resources>  
  10.     <Grid>  
  11.         <TextBlock Text="Value Converter Exampkle" HorizontalAlignment="Stretch" VerticalAlignment="Top" TextAlignment="Center"></TextBlock>  
  12.         <TextBlock Text="First Name" HorizontalAlignment="Left" VerticalAlignment="Top" TextAlignment="Center" Margin="10,38,0,0"></TextBlock>  
  13.         <TextBox Name="txtFirstName" HorizontalAlignment="Left" VerticalAlignment="Top" Height="25" Width="255" Margin="91,29,0,0" ></TextBox>  
  14.         <TextBlock Text="Last Name" HorizontalAlignment="Left" VerticalAlignment="Top" TextAlignment="Center" Margin="11,68,0,0"></TextBlock>  
  15.         <TextBox Name="txtLastName" HorizontalAlignment="Left" VerticalAlignment="Top" Height="25" Width="255" Margin="91,59,0,0" ></TextBox>  
  16.         <TextBlock Text="Full Name" HorizontalAlignment="Left" VerticalAlignment="Top" TextAlignment="Center" Margin="14,105,0,0"></TextBlock>  
  17.         <TextBox Name="txtFullName" HorizontalAlignment="Left" VerticalAlignment="Top" Height="25" Width="332" Margin="14,126,0,0" >  
  18.             <TextBox.Text>  
  19.                 <MultiBinding Converter="{StaticResource fullNameConverter}">  
  20.                     <Binding ElementName="txtFirstName" Path="Text" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"></Binding>  
  21.                     <Binding ElementName="txtLastName" Path="Text" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"></Binding>  
  22.                 </MultiBinding>  
  23.             </TextBox.Text>  
  24.         </TextBox>  
  25.         <Button Content="Click" HorizontalAlignment="Left" VerticalAlignment="Top" Height="23" Width="50" Margin="165,171,0,0">  
  26.             <Button.IsEnabled>  
  27.                 <MultiBinding Converter="{StaticResource multivalueConverter}">  
  28.                     <Binding ElementName="txtFirstName" Path="Text"></Binding>  
  29.                     <Binding ElementName="txtLastName" Path="Text"></Binding>  
  30.                 </MultiBinding>  
  31.             </Button.IsEnabled>  
  32.         </Button>  
  33.         
  34.     </Grid>  
  35.       
  36. </Window>  
Summery
I hope you now have some understanding of converters and the type of converters in WPF.
 

Up Next
    Ebook Download
    View all
    Learn
    View all