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