How to: Produce a Value Based on a List of Bound Items

MultiBinding allows you to bind a binding target property to a list of source properties and then apply logic to produce a value with the given inputs. This example demonstrates how to use MultiBinding.


In the following example, NameListData refers to a collection of PersonName objects, which are objects that contain two properties, firstName and lastName. The following example produces a TextBlock that shows the first and last names of a person with the last name first.

  Title="MultiBinding Sample">
    <c:NameList x:Key="NameListData"/>
    <c:NameConverter x:Key="myNameConverter"/>
<TextBlock Name="textBox2" DataContext="{StaticResource NameListData}">
    <MultiBinding Converter="{StaticResource myNameConverter}"
      <Binding Path="FirstName"/>
      <Binding Path="LastName"/>

To understand how the last-name-first format is produced, let's take a look at the implementation of the NameConverter:

public class NameConverter : IMultiValueConverter
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        string name;

        switch ((string)parameter)
            case "FormatLastFirst":
                name = values[1] + ", " + values[0];
            case "FormatNormal":
                name = values[0] + " " + values[1];

        return name;

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        string[] splitValues = ((string)value).Split(' ');
        return splitValues;
Public Class NameConverter
    Implements IMultiValueConverter

Public Function Convert1(ByVal values() As Object, _
                         ByVal targetType As System.Type, _
                         ByVal parameter As Object, _
                         ByVal culture As System.Globalization.CultureInfo) As Object _
                         Implements System.Windows.Data.IMultiValueConverter.Convert
    Select Case CStr(parameter)
        Case "FormatLastFirst"
            Return (values(1) & ", " & values(0))
    End Select
    Return (values(0) & " " & values(1))
End Function

Public Function ConvertBack1(ByVal value As Object, _
                             ByVal targetTypes() As System.Type, _
                             ByVal parameter As Object, _
                             ByVal culture As System.Globalization.CultureInfo) As Object() _
                             Implements System.Windows.Data.IMultiValueConverter.ConvertBack
    Return CStr(value).Split(New Char() {" "c})
End Function
End Class

NameConverter implements the IMultiValueConverter interface. NameConverter takes the values from the individual bindings and stores them in the values object array. The order in which the Binding elements appear under the MultiBinding element is the order in which those values are stored in the array. The value of the ConverterParameter attribute is referenced by the parameter argument of the Converter method, which performs a switch on the parameter to determine how to format the name.

See Also

Convert Bound Data
Data Binding Overview
How-to Topics