WPF binding to two properties

binding, wpf

Solution

Try use the `MultiBinding`:

Describes a collection of Binding objects attached to a single binding target property.

Example:

`XAML`

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

`Converter`

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];
                break;
            case "FormatNormal":
                default:
                name = values[0] + " " + values[1];
                break;
        }

        return name;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        string[] splitValues = ((string)value).Split(' ');
        return splitValues;
    }
}

Problem

I have a WPF control that has a `Message` property. I currently have this: ``` <dxlc:LayoutItem > <local:Indicator Message="{Binding PropertyOne}" /> </dxlc:LayoutItem> ``` But i need that `Message` property to be bound to two properties. Obviously can't be done like this, but this can help explain what it is I want: ``` <dxlc:LayoutItem > <local:Indicator Message="{Binding PropertyOne && Binding PropertyTwo}" /> </dxlc:LayoutItem> ```

Original source

Related problems