WPF IValueConverter – 将多个值转换为单个值

我正在努力维护别人的代码,因为那个人是WPF专家。 另一方面,我不是。 🙂

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

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

一个简单的例子如下。

现有代码的逻辑说……

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只允许我将一个“东西”用于转换。

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

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