[英]WPF DataGrid Get Cell Value Using Button
我有一个数据网格。 当我单击删除按钮时,我想在数据网格中获取ID列值。 我怎样才能做到这一点?
这是我的数据网格xaml。
<DataGrid x:Name="dtgridUser" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding ID}" Header="ID" />
<DataGridTextColumn Binding="{Binding Name}" Header="Name" />
<DataGridTextColumn Binding="{Binding Age}" Header="Age" />
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button x:Name="btnDelete" Click="btnDelete_Click" >Delete</Button>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
通过将此代码添加到我的按钮中,我解决了它。 基本上,当我单击按钮时。 它将获得单击按钮的行,然后从特定列中选择值。
private void btnDelete_Click(object sender, RoutedEventArgs e)
{
DataGrid dataGrid = YOURDATAGRIDNAME;
DataGridRow Row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(dataGrid.SelectedIndex);
DataGridCell RowAndColumn = (DataGridCell)dataGrid.Columns[0].GetCellContent(Row).Parent;
string CellValue = ((TextBlock)RowAndColumn.Content).Text;
MessageBox.Show(CellValue);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.