4

我现在正在尝试维护其他人的代码,该人是 WPF 专家。另一方面,我不是。:)

该代码使用 IValueConverter 将状态枚举转换为布尔值,该布尔值控制 UserControl 是否显示在屏幕上。

我发现了一个缺点,在这种情况下单个枚举是不够的,实际上还需要考虑另一个布尔值。是否有另一个可以使用的对象将 2 个项目作为参数来进行转换?(“转换器”参数已被使用。)

一个简单的例子如下。

现有代码的逻辑说......

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

我需要考虑另一件事,这将使它如下。

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 只允许我拿一个“东西”进行转换。

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

4

1 回答 1

14

使用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 而不是常规绑定

<MultiBinding Converter="{StaticResource MyMultiValueConverterKey}">
    <Binding Path="Value1" />
    <Binding Path="Value2" />
</MultiBinding>
于 2010-10-20T16:33:01.273 回答