-->

WPF multibinding到视图模型?(wpf multibinding to viewmod

2019-08-17 19:20发布

对于我的生活,我似乎无法绑定到使用multibindings我的视图模型。 所有在网络上绑定的例子来直接GUI元素,但每当我试图用视图模型对象抛出异常。

我的问题是,我该如何在XAML中添加multibinding几个视图模型对象?

我需要一个上下文菜单的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对象。

Answer 1:

菲利普的回答是可以接受的,但为寻找菲利普期望的解决方案,下面应该这样做:

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



Answer 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>


Answer 3:

这是一个古老的职位,但我知道我有同样的问题,无法在网络上找到任何解决方案。

我想他是问是怎么使用不绑定到一个GUI元素,而不是绑定到视图模型不止一个属性multibinding。

事情是这样的:

XAML:

<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解决方案。



Answer 4:

Bluebit你的等待已经结束...创建目标控制,而不是风格。

下面是一个例子,我有一个按钮,这是需要有如果组合框( 称为comboConfig)不选择尚未被禁用的登录按钮(选择指数-1),或者如果在我的ViewModel一个布尔值设置为真(LoginInProcess)。 对于视图模型布尔我只需将其路径为成员名称属性,它是在风格的时间到窗口的DataContext绑定:

<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>


文章来源: wpf multibinding to viewmodel?