Silverlight DataGrid Current Item as Click.CommandParameter
Posted by Dave Bouwman | Posted in M-V-VM, Prism 2, Silverlight | Posted on 02-07-2009
0
Note: This applies to Prism v2 with Silverlight 3 Beta.
So I’ve added a button into a column of my DataGrid, and I want to bind in the Click handler to my ViewModel, as well as pass the selected item into said handler.
I’m not going to go into the details of Commanding in Prism at this time (maybe later), but here’s what the xaml looks like:
...
<data:DataGridTemplateColumn Header="Details"> <data:DataGridTemplateColumn.CellTemplate> <DataTemplate> <Button Commands:Click.Command="{Binding Path=Value, Source={StaticResource ViewDetailsCommand}}" Commands:Click.CommandParameter="{Binding}" Cursor="Hand" Content="Details" /> </DataTemplate> </data:DataGridTemplateColumn.CellTemplate> </data:DataGridTemplateColumn>
...
The Click.Command is a little out of the ordinary in that the Binding Path is set to Value, and the Source is a StaticResource on the control. There are a few hoops to jump through on this, but the Prism reference implementation application (RIStockTrader) does show how this works.
What had me scratching my head was how to send in the object which represents the current row in the grid. All of the examples looked like this:
Commands:Click.CommandParameter="{Binding Path=ItemId}"
Which is great when you just want to send the Id of the item in. But in my case, I want the actual object so I can send that to the "Details" form for editing. In hind sight it’s obvious that you would set the CommandParameter to the "Binding", since that’s the item itself, but it was one of those things that you don’t think would make sense.
Anyhow – through the power of Google, I hope this saves someone else some time/frustration.

