WPF: using IValueConverter

Posted by dariosantarelli on July 15, 2007

IValueConverter is a very useful interface (defined into the System.Windows.Data namespace in presentationframework.dll) that allows binding between different property types in WPF. Here is an example: we need a simple and reusable way to bind our custom properties (orders properties precisely) to a Listbox presentation properties. Well, by implementing the IValueConverter interface, for each ListBoxItem we can convert the value of the Priority property of our business object Order in the value of the Foreground property of the corresponding TextBlock which shows the Order name.
So, let’s define our Order class:

public class Order { private string _Name; private Priority _Priority; public Order() {} public Order(string name, Priority priority) { _Name = name; _Priority = priority; } public string Name { get { return _Name; } set { _Name = value; } } public Priority Priority { get { return _Priority; } set { _Priority = value; } } } public enum Priority : short { High=2,Normal=1,Low=0 } }

Now, by using XAML markup code we define a PriorityConverter class as a Converter inside the binding towards the Foreground property of the TextBlock containing the order name, while in the codebehind we define the PriorityConverter class as IValueConverter interface implementation:

<Window ... xmlns:mydata="clr-namespace:MyNamespace"> <Window.Resources> <DataTemplate DataType="{x:Type mydata:Order}"> <StackPanel Orientation="Vertical"> <TextBlock Text="{Binding Path=Name}"> <TextBlock.Foreground> <Binding Path="Priority"> <Binding.Converter> <mydata:PriorityConverter /> </Binding.Converter> </Binding> </TextBlock.Foreground> </TextBlock> </StackPanel> </DataTemplate> </Window.Resources> <StackPanel> <TextBlock>Orders List.</TextBlock> <ListBox Name="ordersListBox" ItemsSource="{Binding}"></ListBox> </StackPanel> </Window>

public class PriorityConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value != null) { Priority priority = (Priority)value; BrushConverter colorConverter = new BrushConverter(); switch (priority) { case Priority.High: return colorConverter.ConvertFromString("#FF0000") as Brush; case Priority.Normal: return colorConverter.ConvertFromString("#000000") as Brush; case Priority.Low: return colorConverter.ConvertFromString("#0000FF") as Brush; default: break; } } return null; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return null; } }

In this case, the ConvertBack method implementation isn’t necessary, because the needed binding is one-way, from business logic layer to presentation layer. Moreover, the BrushConverter object can return an object representing a Brush by invoking the ConvertFromString method on a hexadecimal string representation of a color.
An advantage of this approach is the reusability, because above all it allows very simple implementations in those contexts in which the presentation layer can be completely changed, mantaining the business logic layer unchanged.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: