IMultiValueConverter Interface

Provides a way to apply custom logic in a MultiBinding.

Namespace:  System.Windows.Data
Assembly:  PresentationFramework (in PresentationFramework.dll)

'Declaration
Public Interface IMultiValueConverter

The IMultiValueConverter type exposes the following members.

  NameDescription
Public methodConvertConverts source values to a value for the binding target. The data binding engine calls this method when it propagates the values from source bindings to the binding target.
Public methodConvertBackConverts a binding target value to the source binding values.
Top

To associate a converter with a MultiBinding, create a class that implements the IMultiValueConverter interface, and then implement the Convert and ConvertBack methods.

Individual bindings in the collection can have their own value converters. For more information, see IValueConverter.

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.

<Window
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:c="clr-namespace:SDKSample"
  x:Class="SDKSample.Window1"
  Width="400"
  Height="280"
  Title="MultiBinding Sample">
	
  <Window.Resources>
    <c:NameList x:Key="NameListData"/>
    <c:NameConverter x:Key="myNameConverter"/>


...


</Window.Resources>


...


<TextBlock Name="textBox2" DataContext="{StaticResource NameListData}">
  <TextBlock.Text>
    <MultiBinding Converter="{StaticResource myNameConverter}"
                  ConverterParameter="FormatLastFirst">
      <Binding Path="FirstName"/>
      <Binding Path="LastName"/>
    </MultiBinding>
  </TextBlock.Text>
</TextBlock>


...


</Window>

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

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.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft