Sujeet Suman

Sujeet Suman

  • 386
  • 4.1k
  • 1.5m

How to bind a datagrid button using mvvm?

Aug 13 2020 5:20 AM
I have a datagrid & column contains a button. I would like to call a method in ViewModel on button click event.
 
<DataGrid x:Name="dgvEmp" HorizontalAlignment="Left" Height="120" Margin="10,10,0,0" VerticalAlignment="Top" Width="530"
CanUserAddRows="False" AutoGenerateColumns="False" ItemsSource="{Binding Employees}"
Style="{StaticResource styleDataGrid}">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding ID}" Width="110*"/>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="110*"/>
<DataGridTextColumn Header="Address" Binding="{Binding Address}" Width="110*"/>
<DataGridTemplateColumn Width="110*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Width="110"
Content="{Binding Path=ConnectionStatus}"
Command="{Binding  UpdateDetailsCommand, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
CommandParameter="{Binding Path=MACAddress}">
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
 

Answers (5)