c# - WPF 工具包 DataGrid SelectionChanged 获取单元格值

标签 c# wpf datagrid wpfdatagrid

请帮助我,我正在尝试从 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/

相关文章:

c# - 如何在编辑 DataGridView 单元格周围绘制边框?

c# - 系统 namespace 在全新安装时缺少 MonoDevelop

c# - 如何使用代码在 C# 中调用 materialDesign 组件并添加到 wpf 设计中?

c# - 如何为给定字符串生成 "random constant colour"?

c# - WPF数据网格按所选列自动排序

c# - 在 DataGrid 中看不到文本框文本

c# - 当可见性改变时,mdi 子窗体绘制缓慢

c# - 控制哪些项目离开堆栈面板的可见区域

C# WPF Datagrid 如何禁用特定列上的选择单元格

C# List<string> 排序问题