I want to execute a command when the user selects a row in a DataGrid.
I see it is possible to wrap cell contents in buttons (although I don't want the button style) – but I don't want to do it at the cell-level.
I also see it's possible to use behaviours to link a command to an event. But preferably I should not have to resort to behaviors for such a common task.
Is it possible to do this via plain old command databinding?
So: 1) user clicks DataGrid row 2) command on view model is fired.
Best Answer
You should use "Interactivity" assembly and
SelectionChanged
event.Where "i" is namespace:
Also you can write binding to
SelectedItem
property of the DataGrid and in the set accessor you can invoke your command, but the first solution that i presented you above is better.If you want to invoke command from main view model and pass
SelectedItem
fromDataGrid
you can useCommadParameter
:When items has own command you can use following code:
Or if elements has own view model that is assigned to it's
DataContext
, you can use following code: