如何向 xaml 中的多个视图模型对象添加多重绑定?
我需要绑定IsEnabled
上下文菜单的属性为我的视图模型中的两个整数。以下绑定不起作用,因为它是为 GUI 组件设计的。我该如何做才能与我的ints一起工作?
<MenuItem ItemsSource="{Binding MyMenuItem}">
<MenuItem.IsEnabled>
<MultiBinding>
<Binding ElementName="FirstInt" Path="Value" />
<Binding ElementName="SecondInt" Path="Value" />
</MultiBinding>
</MenuItem.IsEnabled>
</MenuItem>
MyMenuItem 是具有两个整数 FirstInt 和 SecondInt 的 CLR 对象。
菲利普的答案是可以接受的,但对于任何寻找菲利普所需解决方案的人来说,以下应该可以做到:
<MenuItem ItemsSource="{Binding MyMenuItem}">
<MenuItem.IsEnabled>
<MultiBinding Converter="{StaticResource IntsToEnabledConverter}">
<Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=Window}" Path="DataContext.FirstInt" />
<Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=Window}" Path="DataContext.SecondInt" />
</MultiBinding>
</MenuItem.IsEnabled>
</MenuItem>
应该指出的是,AncestorType
绑定上的内容可能需要相应更改。我假设视图模型设置为DataContext
窗口的,但同样的想法适用于用户控件等。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)