views:

417

answers:

2

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.

+2  A: 

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>
Bryan Anderson
A: 

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.

Filip