Reputation: 4892
I am fairly new to WPF and am struggling a little with a scenario. I have a menu which has menu items. When one of these menu items gets clicked a method needs to be called that will do something based upon the text displayed associated with that menu item. So for example, the menu item's content was "test" so I would need to do something with "test". FYI, this "something" directly affects a collection on the ViewModel.
This is easy to achieve using the click event and no ViewModel, but I was trying to implement MVVM using an explicit ViewModel. So I started to look into Commands but cannot see how I would pass anything from the View back into the Command in the ViewModel.
Any suggestions on what I should be doing here?
Thanks
Upvotes: 3
Views: 9042
Reputation:
You have two choices.
Use the CommandParameter to send information back to the Command, such as
<MenuItem Command="{Binding MenuCommand}" CommandParameter="File">File</MenuItem>
I think the first option is better, as it avoids magic strings.
Upvotes: 2
Reputation: 24453
Given that you have a collection of items driving the commanding I would recommend using something similar to Will's second suggestion like this:
<MenuItem
Command="{Binding MenuCommand}"
CommandParameter="{Binding}"
Header="{Binding DisplayText}" />
On the ViewModel side you can use a DelegateCommand or RelayCommand to hook up your handler method. This allows you to inspect the menu item's data and do whatever you need to based on what's in it.
public DelegateCommand<MyMenuObject> MenuCommand { get; private set; }
public ViewModel()
{
MenuCommand = new DelegateCommand<MyMenuObject>(MenuCommandExecuted);
}
public void MenuCommandExecuted(MyMenuObject parameter)
{
// do something based on the parameter object
}
You would also need to either pass MenuCommand into an ICommand property on your collection items to use the simple Binding form above or change the command binding to use something like a RelativeSource FindAncestor to look up the tree to the parent's DataContext (ViewModel) for the MenuCommand. Using this approach gives you flexibility to add or remove items from the collection and not need to make any UI changes to support the new set of items.
Upvotes: 5