40

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>
mo alaz
  • 4,529
  • 8
  • 30
  • 36

3 Answers3

43
<TextBlock.Text>
    <MultiBinding StringFormat="{}{0} {1}">
        <Binding Path="FirstName"/>
        <Binding Path="LastName"/>
    </MultiBinding>
</TextBlock.Text>
adPartage
  • 829
  • 7
  • 12
40

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;
    }
}
Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
8

You can't do And operation in XAML.

Create wrapper property in your view model class which will return and of two properties and bind with that property instead.

public bool UnionWrapperProperty
{
   get
   {
      return PropertyOne && PropertyTwo;
   }
}

XAML

<local:Indicator Message="{Binding UnionWrapperProperty}" />

Another approach would be to use MultiValueConverter. Pass two properties to it and return And value from the converter instead.

Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • What you have posted isn't a `Bitwise AND`. It's just an `AND` operator. In any case, I think he wanted whatever the type is, concatenated. – gleng Apr 22 '14 at 17:18
  • 2
    Yeah. But OP haven't mentioned anything in question and since we can't `&&` strings (or all types). So, was just assuming OP wants bool property. Will update once OP clears the picture. – Rohit Vats Apr 22 '14 at 17:22