Handling command on/off on row selection

Topics: Prism v2 - WPF 3.5
May 24, 2010 at 3:04 PM

Hi there,

I have a view with a grid of data, where multiple rows can be selected.

I also have a DelegateCommand in my VM that I need to enable if only one row is selected. If user selects multiple rows, it should be disabled. The command is bound in 2 places - the main view with the grid as a context menu option, and another view - the ribbon button group

The problem is that the information about the currently selected rows is part of the main view, not the view model or the ribbon.

I'm thinking I need handle the grid's SelectionChanged event inside the view's codebehind and then somehow signal the VM which rows have been selected. The VM would enable/disable the command. This would in turn enable/disable the context menu and the ribbon button.

I'm wondering if there is an easier or a more unified/universal way of accomplishing it.



May 28, 2010 at 9:34 PM


Handling the grid’s SelectionChanged event inside the view's code behind is a possible way to expose that information to the ViewModel. You can read this blog post by Glenn, in which he talks about code behind in MVVM. On the other hand, you could use the CommandInvokeAction behavior. That would have the SelectionChanged event tied together, so that when the event is raised, an associated command is called.

I hope you find this helpful.

Guido Leandro Maliandi