tags:

views:

440

answers:

2

I'm working in the MVVM design patter with WPF. I have a context menu 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 selection changed 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

A: 

sorry this isn't a comment, i just can't comment yet, but:

do you mean just: listView1.SelectionChanged += ..

?

i guess not.. but i didn't understand..

Itay
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
A: 

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:

Jalfp