Posted by: Zeeshan Amjad | December 12, 2011

Multi value converter and Markup extension


I came across a question that I have a class with two properties, such as FirstName and LastName, but I want to display the value of these in one control such as TextBlock, how can I do this. In fact there are more than one way to do this. The simplest solution is to do a multi binding and implement IMultiValueConverter nterface. Here is a simple implementation of our interface.

Code Snippet
[ValueConversion(typeof(String), typeof(String))]
public class MultiValue : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        string firstName = values[0] as string;
        string lastName = values[1] as string;

        return string.Format("{0}, {1}", lastName, firstName);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

 

We create an object of it in window resource.

Code Snippet
<local:MultiValue x:Key="multiValue"/>

 

And here is the usage of it.

Code Snippet
<TextBlock Margin="5">
    <TextBlock.Text>
        <MultiBinding Converter="{StaticResource multiValue}">
            <Binding Path="FirstName"/>
            <Binding Path="LastName"/>
                            </MultiBinding>
    </TextBlock.Text>
</TextBlock>

But like lots of other WPF thing, here we can do it in more than one way. We can create a markup extension which implement the IMultiValueConverter interface. We already saw few examples of markup extension which implement IValueConverter here and here.

Here is a markup extension to implement IValueConverter.

Code Snippet
[ValueConversion(typeof(String), typeof(String))]
public class MultiValueExtension : MarkupExtension, IMultiValueConverter
{
    public MultiValueExtension()
    {

    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }

    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        string firstName = values[0] as string;
        string lastName = values[1] as string;

        return string.Format("{0}, {1}", lastName, firstName);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

 

Here is a usage of this markup extension.

Code Snippet
<TextBlock Margin="5">
    <TextBlock.Text>
        <MultiBinding Converter="{local:MultiValue}">
            <Binding Path="FirstName"/>
            <Binding Path="LastName"/>
                            </MultiBinding>
    </TextBlock.Text>
</TextBlock>

 

Here is a complete C# code of our program.

Code Snippet
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;

namespace WpfMultiValue
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            Student student = new Student() { FirstName = "Bob", LastName = "Smith" };

            DataContext = student;
        }
    }

    public class Student
    {
        public string FirstName
        { get; set; }

        public string LastName
        { get; set; }
    }
    
    [ValueConversion(typeof(String), typeof(String))]
    public class MultiValueExtension : MarkupExtension, IMultiValueConverter
    {
        public MultiValueExtension()
        {

        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }

        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            string firstName = values[0] as string;
            string lastName = values[1] as string;

            return string.Format("{0}, {1}", lastName, firstName);
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

 

And here is complete XAML code of the program.

Code Snippet
<Window x:Class="WpfMultiValue.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
        xmlns:local="clr-namespace:WpfMultiValue"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBlock Margin="5">
            <TextBlock.Text>
                <MultiBinding Converter="{local:MultiValue}">
                    <Binding Path="FirstName"/>
                    <Binding Path="LastName"/>
                                    </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </Grid>
</Window>

 

MultiValueConverterMarkupExtension

Advertisements

Responses

  1. […] Converter and Markup Extension We saw an example of Multivalue converter markup extension here. Here we created a class inherited by MarkupExtension base class and implement IMultiValueConverter […]


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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Categories

%d bloggers like this: