IMultiValueConverter ConvertBack problem
I'm using wpf + mvvm and am trying to implement a conditional converter. Here is what I'm doing in the xaml:
<CheckBox.IsChecked>
<MultiBinding Converter="{StaticResource pageSourceConverter}">
<Binding Path="CurrentPage.Source"/>
<Binding Path="Project.Type1.MachineTypes.Rotating"/>
开发者_StackOverflow <Binding Path="Project.Type2.MachineTypes.Rotating" />
<Binding Path="Project.Type3.MachineTypes.Rotating" />
<Binding Path="Project.Type4.MachineTypes.Rotating" />
</MultiBinding>
</CheckBox.IsChecked>
And the MultiConverter:
public class PageSourceConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
String pageSource = values[0] as String;
if (pageSource == "Type1")
return values[1];
else if (pageSource == "Type2")
return values[2];
else if (pageSource == "Type3")
return values[3];
else if (pageSource == "Type4")
return values[4];
else
return null;
}
public object[] ConvertBack(object value, Type[] targetType, object parameter, CultureInfo culture)
{
return null;
}
}
So what I want to do is conditionally bind an element on a page to different Models I have in the viewController based on the page type the user is on.
What do I need to do for the convertback? I don't know how to tell which element in the return array the value belongs to. Any ideas?
Using a MultiValueConverter
for this looks like abuse to me, you do not use all your inputs, you just select one, a normal ValueConverter
which takes those 4 objects as ConverterParameter
would probably make more sense, that way you do not need to return values for them in ConvertBack
.
Besides that the ConvertBack
is logically impossible. You bind to IsChecked
which is a boolean/nullable-boolean, giving you two or three states while your input has four states (the different types), so your convert function maps from four values to two or three. There cannot be an inverse function for that.
精彩评论