WPF IValueConverter - Convert multiple values ​​to a single value

I am trying to save another user's code right now when this person is a WPF expert. I, on the other hand, no. :)

The code uses IValueConverter to convert an enumeration of states to a boolean that determines whether the UserControl is displayed on the screen.

I found a drawback that just listing in this circumstance is not enough, in fact there is another logical value that also needs to be taken into account. Is there any other object that could be used to take 2 elements as arguments to convert? (The converter option is already in use.)

Quick example:

The logic of the existing code says ...

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

I need to consider one more thing that would do it as follows.

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

The IValueConverter that will perform the conversion allows me to take one β€œthing” for the conversion.

Any help is appreciated. Thanks,

Mj

+4
source share
1 answer

Use 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(); } } 

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

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

All Articles