WPF IValueConverter - 将多个值转换为单个值 [英] WPF IValueConverter - converting multiple values into a single value

查看:400
本文介绍了WPF IValueConverter - 将多个值转换为单个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想现在保持别人的代码如该人是一个WPF的专家。我,在另一方面,不是。 :)

I'm trying to maintain someone else's code right now where that person is a WPF expert. I, on the other hand, am not. :)

中的代码使用的IValueConverter到状态枚举转换成一个管辖用户控件是否在屏幕上显示一个布尔值。

The code uses the IValueConverter to convert a state enumeration into a boolean which governs whether or not a UserControl is displayed on the screen.

我发现一个缺点,在这种情况下一个枚举是远远不够的,居然还有这么需要考虑,以及另一个布尔值。是否有可能被用于其他对象,将采取2项作为参数,以做转换? (在转换器参数已被使用。)

I've discovered a shortcoming that a single enumeration in this circumstance is not enough, there's actually another boolean that needs to be considered as well. Is there another object that could be used that would take 2 items in as arguments in order to do a conversion? (The "converter" parameter is already being used.)

一个简单的例子是如下。

A quick example would be the following.

现有代码的逻辑说...

The logic of the existing code says...

If it's sunny, go to work.
If it's raining, don't go to work.



我需要别的东西进去这将如下做到这一点。

I need to take another thing into account which would make it as follows.

If it's sunny and you're wearing pants, go to work.
If it's sunny and you're not wearing pants, don't go to work.
If it's raining and you're wearing pants, don't go to work.
If it's raining and you're not wearing pants, don't go to work.

的IValueConverter,这将执行转换只允许我举一个东西中进行转换。

IValueConverter, which would perform the conversion only allows me to take one "thing" in for conversion.

任何帮助表示赞赏。谢谢,

Any help is appreciated. Thanks,

MJ

推荐答案

使用的 IMultiValueConverter

public class MyMultiValueConverter: IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        // Do something with the values array. It will contain your parameters
    }

    public object[] ConvertBack(object values, Type[] targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

您还需要在XAML使用MultiBinding代替一个普通绑定

You also need to use a MultiBinding in the XAML instead of a regular binding

<MultiBinding Converter="{StaticResource MyMultiValueConverterKey}">
    <Binding Path="Value1" />
    <Binding Path="Value2" />
</MultiBinding>

这篇关于WPF IValueConverter - 将多个值转换为单个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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