WPF结合两个属性 [英] WPF binding to two properties

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

问题描述

我有,有一个 WPF控件消息属性。

目前,我有这样的:

 < dxlc:LayoutItem>
            <局部:指示灯消息={结合PropertyOne}/>
 < / dxlc:LayoutItem>

但我需要一个消息属性绑定到两个属性。

显然不能做到这样,但是这可以帮助解释什么是我想要的:

 < dxlc:LayoutItem>
            <局部:指示灯消息={结合PropertyOne&放大器;&安培;结合PropertyTwo}/>
 < / dxlc:LayoutItem>


解决方案

尝试使用<$c$c>MultiBinding:


  

中记录了一个约束性目标属性绑定对象的集合。


例如:

XAML

 &LT;&的TextBlock GT;
   &LT; TextBlock.Text&GT;
       &LT; MultiBinding转换器={StaticResource的myNameConverter}
                     ConverterParameter =FormatLastFirst&GT;
          &LT;绑定路径=名字/&GT;
          &LT;绑定路径=姓氏/&GT;
       &LT; / MultiBinding&GT;
   &LT; /TextBlock.Text>
&LT; / TextBlock的&GT;

转换

 公共类NameConverter:IMultiValueConverter
{
    公共对象转换(对象[]值类型TARGETTYPE,对象参数,CultureInfo的文化)
    {
        字符串名称;        开关((字符串)参数)
        {
            案FormatLastFirst:
                名=值[1] +,+值[0];
                打破;
            案FormatNormal:
                默认:
                名=值[0] ++值[1];
                打破;
        }        返回名称;
    }    公用对象[] ConvertBack(对象的值,类型[] targetTypes,对象参数,CultureInfo的文化)
    {
        字符串[] splitValues​​ =((字符串)值).Split('');
        返回splitValues​​;
    }
}

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>

解决方案

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

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

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