WPF 绑定到两个属性 [英] WPF binding to two properties

查看:27
本文介绍了WPF 绑定到两个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有 Message 属性的 WPF 控件.

I have a WPF control that has a Message property.

我目前有这个:

 <dxlc:LayoutItem >
            <local:Indicator Message="{Binding PropertyOne}" />
 </dxlc:LayoutItem>

但我需要将 Message 属性绑定到两个属性.

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>

推荐答案

尝试使用 MultiBinding:

描述附加到单个绑定目标属性的 Binding 对象集合.

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

示例:

XAML

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

转换器

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;
    }
}

这篇关于WPF 绑定到两个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆