Gets or sets the converter object that is called by the binding engine to modify the data as it is passed between the source and target, or vice versa.
C#
public IValueConverter Converter { get; set; }
XAML
<Binding Converter="converterReference"/>
converterReference
A reference to an existing object that implements IValueConverter and functions as a converter. Typically the object is created in a ResourceDictionary and given a key, then referenced by using the StaticResourcemarkup extension. For instance: <Binding Converter="{StaticResource myConverter}" .../>
public interface IValueConverter
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. |
<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>
using System; using System.Collections.ObjectModel; using System.Windows.Controls; using System.Windows.Data; using Microsoft.Phone.Controls; namespace ConverterParameterEx { public partial class Page : PhoneApplicationPage { public ObservableCollection<Recording> MyMusic = new ObservableCollection<Recording>(); public Page() { InitializeComponent(); // Add items to the collection. MyMusic.Add(new Recording("Chris Sells", "Chris Sells Live", new DateTime(2008, 2, 5))); MyMusic.Add(new Recording("Luka Abrus", "The Road to Redmond", new DateTime(2007, 4, 3))); MyMusic.Add(new Recording("Jim Hance", "The Best of Jim Hance", new DateTime(2007, 2, 6))); // Set the data context for the combo box. MusicCombo.DataContext = MyMusic; } } // Simple business object. public class Recording { public Recording() { } public Recording(string artistName, string cdName, DateTime release) { Artist = artistName; Name = cdName; ReleaseDate = release; } public string Artist { get; set; } public string Name { get; set; } public DateTime ReleaseDate { get; set; } } public class DateFormatter : IValueConverter { // This converts the DateTime object to the string to display. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { // Retrieve the format string and use it to format the value. string formatString = parameter as string; if (!string.IsNullOrEmpty(formatString)) { return string.Format(culture, formatString, value); } // If the format string is null or empty, simply call ToString() // on the value. return value.ToString(); } // No need to implement converting back on a one-way binding public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } }