开发者

WPF binding multiple properties of new complex type

Is it possible to do something like this in WPF? Presuming I have a class like this:

class Pair
{
    public string A{get;set;}
    public string B{get;set;}
}

And properties PropertyA correctly bound, PropertyB correctly bound...

<开发者_如何学编程SomeControl Tag="{Pair A="{Binding Path=PropertyA}", B="{Binding Path=PropertyB}" />


You can use a MultiBinding with a converter:

<SomeControl>
    <SomeControl.Tag>
        <MultiBinding Converter="{StaticResource pairConverter}">
            <Binding Path="A" />
            <Binding Path="B" />
        </MultiBinding>
    </SomeControl.Tag>
</SomeControl>

With this converter:

public class PairConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values.Length == 2)
        {
            return new Pair { A = (string)values[0], B = (string)values[1] };
        }
        return Binding.DoNothing;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        Pair p = (Pair)value;
        return new object[] { p.A, p.B };
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜