6

对于我的生活,我似乎无法使用多重绑定绑定到我的视图模型。网络上的所有示例都直接绑定到 gui 元素,但是每当我尝试使用 viewmodel 对象时,都会抛出异常。

我的问题是,如何向 xaml 中的多个视图模型对象添加多重绑定?

我需要将上下文菜单的 IsEnabled 属性绑定到我的视图模型中的两个整数。以下绑定不起作用,因为它是为 GUI 组件设计的。我将如何使用我的整数?

<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 对象。

4

4 回答 4

19

Filip 的回答是可以接受的,但对于任何寻找 Filip 所需解决方案的人来说,应该这样做:

<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窗口,但同样的想法也适用于用户控件等。

于 2014-06-13T04:47:51.747 回答
2

对于您的特定示例,您需要一个 IMultiValueConverter 将两个整数转换为表示菜单项是否启用的布尔值。像这样的东西:

Public Class MVCIntsToEnabled
    Implements IMultiValueConverter

    Public Function Convert(ByVal values() As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IMultiValueConverter.Convert
        If values IsNot Nothing Then
            If values.Count = 2 Then
                Return (values(0) > 0) AndAlso (values(1) > 0)
            Else
                Return False
            End If
        Else
            Throw New ArgumentNullException("values")
        End If
    End Function

    Public Function ConvertBack(ByVal value As Object, ByVal targetTypes() As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object() Implements System.Windows.Data.IMultiValueConverter.ConvertBack
        Throw New NotImplementedException()
    End Function

End Class

像这样使用:

<local:MVCIntsToEnabled x:Key="IntsToEnabledConverter"  />

...

<MenuItem ItemsSource="{Binding MyMenuItem}">
    <MenuItem.IsEnabled>
        <MultiBinding  Converter="{StaticResource IntsToEnabledConverter}">
            <Binding ElementName="FirstInt" Path="Value" />
            <Binding ElementName="SecondInt" Path="Value" />
        </MultiBinding>
    </MenuItem.IsEnabled>
</MenuItem>
于 2009-10-12T14:21:51.347 回答
2

这是我知道的旧帖子,但我有同样的问题,在网上找不到任何解决方案。

我认为他要问的是如何使用不绑定到 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 解决方案,我会有更好看的代码。

于 2010-10-05T04:09:22.763 回答
1

Bluebit 你的等待已经结束了……为你的目标控件创建一个样式。

这是一个示例,其中我有一个登录按钮,如果组合框(名为 comboConfig)还没有选择(选定索引 -1)或者我的 ViewModel 上的布尔值设置为,则需要禁用该按钮真(登录进程)。对于 ViewModel 布尔值,我只需将其路径设置为成员名称属性,该属性在样式时绑定到 Windows 数据上下文:

<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
    <Style.Triggers>
        <DataTrigger Value="True">
            <DataTrigger.Binding>
                <MultiBinding Converter="{StaticResource MultiComboBoolBoolFalse}">
                    <Binding ElementName="comboConfig" Path="SelectedIndex" />
                    <Binding Path="LoginInProcess"/>
                </MultiBinding>
            </DataTrigger.Binding>
            <Setter Property="IsEnabled" Value="False"/>
        </DataTrigger>
    </Style.Triggers>
</Style>
于 2013-01-15T17:49:48.390 回答