请帮助我,我正在尝试从 SelectionChangedEvent 中的选定行获取 Cell[0] 的值。
我只是设法获得许多不同的 Microsoft.Windows.Controls,希望我遗漏了一些愚蠢的东西。
希望我能从这里得到一些帮助...
private void datagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Microsoft.Windows.Controls.DataGrid _DataGrid = sender as Microsoft.Windows.Controls.DataGrid;
}
我希望它会像...
_DataGrid.SelectedCells[0].Value;
但是 .Value 不是一个选项....
非常非常感谢,这让我发疯了! 丹
最佳答案
代码少,效果好。
private void datagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DataGrid dataGrid = sender as DataGrid;
DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(dataGrid.SelectedIndex);
DataGridCell RowColumn = dataGrid.Columns[ColumnIndex].GetCellContent(row).Parent as DataGridCell;
string CellValue = ((TextBlock)RowColumn.Content).Text;
}
ColumnIndex 是你想知道的列的索引。
关于c# - WPF 工具包 DataGrid SelectionChanged 获取单元格值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2148978/