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:
It should be noted that the
AncestorType
on the binding may need to be changed accordingly. I assumed the view model was set as theDataContext
of the window, but the same idea applies to user controls, etc.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:
Used like this:
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:
ViewModel:
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:
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.
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: