MultiBinding Class
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace: http://schemas.microsoft.com/winfx/2006/xaml/presentation
'Declaration <ContentPropertyAttribute("Bindings")> _ Public Class MultiBinding Inherits BindingBase Implements IAddChild 'Usage Dim instance As MultiBinding
/** @attribute ContentPropertyAttribute("Bindings") */
public class MultiBinding extends BindingBase implements IAddChild
ContentPropertyAttribute("Bindings") public class MultiBinding extends BindingBase implements IAddChild
<MultiBinding> Bindings </MultiBinding>
You can specify multiple bindings in a MultiBinding object. When you use the MultiBinding object with a converter, it produces a final value for the binding target based on the values of those bindings. For example, color might be computed from red, blue, and green values, which can be values from the same or different binding source objects. When a value moves from the target to the sources, the target property value is translated to a set of values that are fed back into the bindings.
The values of the Mode and UpdateSourceTrigger properties determine the functionality of the MultiBinding and are used as the default values for all the bindings in the collection unless an individual binding overrides these properties. For example, if the Mode property on the MultiBinding object is set to TwoWay, then all the bindings in the collection are considered TwoWay unless you set a different Mode value on one of the bindings explicitly. For more information, see the Binding.Mode and Binding.UpdateSourceTrigger properties.
MultiBinding currently supports only objects of type Binding and not MultiBinding or PriorityBinding.
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.
For the complete sample, see Implementing Parameterized MultiBinding Sample.
For another example of an IMultiValueConverter implementation, see Data Binding Demo.
System.Windows.Markup.MarkupExtension
System.Windows.Data.BindingBase
System.Windows.Data.MultiBinding
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.