wpf multibinding to viewmodel?

2020-07-14 12:22发布

For the life of me, I can't seem to bind to my viewmodel using multibindings. All of the examples on the net bind to gui elements directly, but whenever I try with a viewmodel object exceptions are thrown.

My question is, how do I add a multibinding to several viewmodel objects in xaml?

I need to bind the IsEnabled property of a context menu to two integers in my viewmodel. The following binding doesn't work, since its designed for GUI components. How would I do it to work with my ints?

<MenuItem ItemsSource="{Binding MyMenuItem}">
    <MenuItem.IsEnabled>
        <MultiBinding>
            <Binding ElementName="FirstInt" Path="Value" />
            <Binding ElementName="SecondInt" Path="Value" />
        </MultiBinding>
    </MenuItem.IsEnabled>
</MenuItem>

MyMenuItem is CLR object with the two integers, FirstInt and SecondInt.

4条回答
forever°为你锁心
2楼-- · 2020-07-14 12:40

Filip's answer was acceptable, but for anyone looking for Filip's desired solution, the following should do it:

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

It should be noted that the AncestorType on the binding may need to be changed accordingly. I assumed the view model was set as the DataContext of the window, but the same idea applies to user controls, etc.

查看更多
再贱就再见
3楼-- · 2020-07-14 12:41

For your particular example you need an IMultiValueConverter that will convert the two integers to a boolean value representing whether the menu item is enabled or not. Something like this:

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

Used like this:

<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>
查看更多
forever°为你锁心
4楼-- · 2020-07-14 12:42

This is an old post I know but I have the same problem and couldn't find any solution on the net.

I think what he is asking is how to use a multibinding which doesn't bind to a GUI element instead binds to more than one property in the view model.

Something like this:

Xaml:

<MenuItem ItemsSource="{Binding MyMenuItem}">
    <MenuItem.IsEnabled>
        <MultiBinding  Converter="{StaticResource IntsToEnabledConverter}">
            <Binding Source="{Binding FirstInt}" />
            <Binding Source="{Binding SecondInt}" />
        </MultiBinding>
    </MenuItem.IsEnabled>
</MenuItem>

ViewModel:

public class ViewModel
{
 public int FirstInt{ get { return _firstInt;}}
 public int SecondInt{ get { return _secondInt;}}

}

I haven't been able to figure this out either. Instead I used a SingleValueConverter and a binding to a parent object which holds both the variables FirstInt and SecondInt and in the converter use this parent, smth like:

 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();
        }
    }

It's not as clean as if a Multibinding as the parent could be a bigger object with many more members but it worked in my case. I would have better looking code if I could use the Multibinding solution.

查看更多
在下西门庆
5楼-- · 2020-07-14 12:50

Bluebit your wait is over...create a style for your target control instead.

Here is an example where I have a button which is a Login button which needs to be disabled if a combo box (named comboConfig) does not have a selection yet (selected index -1) or if a boolean value on my ViewModel is set to true (LoginInProcess). For the ViewModel boolean I simply set the its path to be the member name property, which is bound at the style time to the windows 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>
查看更多
登录 后发表回答