I'm working in the MVVM design pattern with WPF. I have a
ContextMenu with several items in it on a
ListView. Based on the number of items selected in the
ListView, I want to enable/disable certain
MenuItems. Is there a way to route the
SelectionChanged event along with the number of selected items in the
ListView directly to the view model. If so, I can define a dependency property in the VM for
IsEnabled quite easily. I'm just trying to avoid code-behind to handle this.
You can use an attached behavior to route the SelectionChanged event to your VM. Basically, you create an attached property of type bool. When this property is set to true, you register an event handler for the SelectionChanged event of the target Menu.
Then an attached property can contains the command to execute (databound to a RelayCommand-like command in your VM).
Check those posts for more details: