ListView.SelectionChanged to RoutedCommand

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.

Kelly

13.10.2009 15:08:06
Do you mean just: listView1.SelectionChanged += .. ? i guess not.. but i didn't understand..
Letterman 13.10.2009 15:20:15
I'm aware of how to hook up the event with code behind. What I'm trying to do is handle the event via a RoutedCommand in the ViewModel without any code behind. Thanks, though.
Flacmonster 13.10.2009 15:41:34
1 ОТВЕТ

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:

1
13.10.2009 16:00:38