I have the following DataGrid
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn Header="Registro" Binding="{Binding NumeroRegistro}" />
<DataGridTextColumn Header="Nome" Binding="{Binding Nome}" Width="*" />
<DataGridTextColumn Header="Login" Binding="{Binding Login}" Width="200" />
<DataGridTemplateColumn Width="30" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Name="btnEditarFisioterapeuta" CommandParameter="{Binding Id}" Click="btnEditarFisioterapeuta_Click">
<Rectangle>
<Rectangle.Fill>
<VisualBrush Visual="{StaticResource appbar_page_edit}" />
</Rectangle.Fill>
</Rectangle>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
And, in the code behind I populate the items of the data grid like this
dgFisioterapeuta.ItemsSource = listOfEntities;
Nothing special. Note that I’m binding the Id of the entity in the Button, so that in the code behind I can get this Id and edit the entity. I was wondering, if the whole DataGrid is bound to a list of entities, why not pass the entire entity as CommandParameter?
Is there a way to do it?
In other words, I want to Change the line
<Button Name="btnEditarFisioterapeuta" CommandParameter="{Binding Id}" >
to something like this.
<Button Name="btnEditarFisioterapeuta" CommandParameter="{Binding The_binding_that_represents_the_entity_of_this_row}" >
Yes, you can do that, it’s very simple:
(Note that when you write
{Binding Id}that’s shorthand for{Binding Path=Id}, so to get the whole entity you simply exclude the path.)