WPF binding to two properties

前端 未结 3 1671
耶瑟儿~
耶瑟儿~ 2020-12-15 04:08

I have a WPF control that has a Message property.

I currently have this:

 
            

        
相关标签:
3条回答
  • 2020-12-15 04:10
    <TextBlock.Text>
        <MultiBinding StringFormat="{}{0} {1}">
            <Binding Path="FirstName"/>
            <Binding Path="LastName"/>
        </MultiBinding>
    </TextBlock.Text>
    
    0 讨论(0)
  • 2020-12-15 04:12

    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.

    0 讨论(0)
  • 2020-12-15 04:15

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题