IMultiValueConverter ConvertBack问题
我正在使用 wpf + mvvm 并尝试实现一个条件转换器。这是我在 xaml 中所做的:
<CheckBox.IsChecked>
<MultiBinding Converter="{StaticResource pageSourceConverter}">
<Binding Path="CurrentPage.Source"/>
<Binding Path="Project.Type1.MachineTypes.Rotating"/>
<Binding Path="Project.Type2.MachineTypes.Rotating" />
<Binding Path="Project.Type3.MachineTypes.Rotating" />
<Binding Path="Project.Type4.MachineTypes.Rotating" />
</MultiBinding>
</CheckBox.IsChecked>
和 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;
}
}
所以我想要做的是根据用户所在的页面类型有条件地将页面上的元素绑定到 viewController 中的不同模型。
我需要为convertback做什么?我不知道如何判断该值属于返回数组中的哪个元素。有什么想法吗?
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"/>
<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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
MultiValueConverter
对我来说似乎是滥用,您不使用所有输入,您只需选择一个,一个普通的ValueConverter
,它将这 4 个对象作为ConverterParameter
可能更有意义,这样您就不需要在ConvertBack
中返回它们的值。除此之外,
ConvertBack
在逻辑上是不可能的。您绑定到 IsChecked ,它是一个布尔值/可空布尔值,为您提供两个或三个状态,而您的输入有四个状态(不同类型),因此您的转换函数从四个值映射到两个或三个。不可能有反函数。Using a
MultiValueConverter
for this looks like abuse to me, you do not use all your inputs, you just select one, a normalValueConverter
which takes those 4 objects asConverterParameter
would probably make more sense, that way you do not need to return values for them inConvertBack
.Besides that the
ConvertBack
is logically impossible. You bind toIsChecked
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.