1

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

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Flacmonster
  • 11
  • 1
  • 3
  • Do you mean just: listView1.SelectionChanged += .. ? i guess not.. but i didn't understand.. – Letterman Oct 13 '09 at 15:20
  • 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 Oct 13 '09 at 15:41

1 Answers1

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:

japf
  • 6,598
  • 3
  • 33
  • 30