这是我知道的旧帖子,但我有同样的问题,在网上找不到任何解决方案。
我认为他要问的是如何使用不绑定到 GUI 元素的多重绑定,而是绑定到视图模型中的多个属性。
像这样的东西:
xml:
<MenuItem ItemsSource="{Binding MyMenuItem}">
<MenuItem.IsEnabled>
<MultiBinding Converter="{StaticResource IntsToEnabledConverter}">
<Binding Source="{Binding FirstInt}" />
<Binding Source="{Binding SecondInt}" />
</MultiBinding>
</MenuItem.IsEnabled>
</MenuItem>
视图模型:
public class ViewModel
{
public int FirstInt{ get { return _firstInt;}}
public int SecondInt{ get { return _secondInt;}}
}
我也无法弄清楚这一点。相反,我使用 SingleValueConverter 和绑定到包含变量 FirstInt 和 SecondInt 的父对象,并在转换器中使用这个父对象,例如:
public class IntsToEnabledConverter :IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Parent parent = value as Parent;
if (parent.FirstInt == 1 && parent.SecondInt == 1)
return true;
else
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
它不像父级的 Multibinding 可能是具有更多成员的更大对象那样干净,但它在我的情况下有效。如果我可以使用 Multibinding 解决方案,我会有更好看的代码。