Bind a Property to More Than 1 Binding Source

By Michael Detras

This shows how to bind a dependency property to more than 1 binding source using multi-binding.

Sometimes, we need to bind a dependency property to more than 1 binding source. For example, we want a TextBox to contain the first and last names of a person. Assuming we have FirstName and LastName properties of a Person object, we can bind them to the Text property using MultiBinding.

<TextBox>
<TextBox.Text>
<MultiBinding Converter="{StaticResource NamesConverter}">
<Binding Path="Person.FirstName"/>
<Binding Path="Person.LastName"/>
</MultiBinding>
</TextBox.Text
>
</TextBox>

We just need to define an IMultiValueConverter that will concatenate the strings, and add it to the Resources section.

public class NamesConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return values[0].ToString() + values[1].ToString();
}

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

Related FAQs

Bind properties to your control without the XAML.
Shows an example on how to bind to a property of a UI element using XAML.
Bind a Property to More Than 1 Binding Source  (2537 Views)