IValueConverter Interface
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Exposes methods that allow modifying the data as it passes through the binding engine.
Assembly: System.Windows (in System.Windows.dll)
The IValueConverter type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | Convert | Modifies the source data before passing it to the target for display in the UI. |
![]() | ConvertBack | Modifies the target data before passing it to the source object. This method is called only in TwoWay bindings. |
You can create a class that allows you to convert the format of your data between the source and the target by inheriting from IValueConverter. For example, you might want have a list of colors that you store as RGBA values but display them with color names in the UI. By implementing Convert and ConvertBack, you can change the format of the data values as they are passed between the target and source by the binding engine.
The following example shows how to implement the IValueConverter interface and use the converter when data binding to a collection of object.
<phone:PhoneApplicationPage x:Class="ConverterParameterEx.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:local="clr-namespace:ConverterParameterEx" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" mc:Ignorable="d" d:DesignHeight="800" d:DesignWidth="480"> <Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.Resources> <local:DateFormatter x:Key="FormatConverter" /> </Grid.Resources> <ComboBox Height="60" Width="200" x:Name="MusicCombo" ItemsSource="{Binding}"> <ComboBox.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock FontWeight="Bold" Text="{Binding Path=Name, Mode=OneWay}" /> <TextBlock Text="{Binding Path=Artist, Mode=OneWay}" /> <TextBlock Text="{Binding Path=ReleaseDate, Mode=OneWay, Converter={StaticResource FormatConverter}, ConverterParameter=\{0:d\}}" /> </StackPanel> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> </Grid> </phone:PhoneApplicationPage>
