【问题标题】:Get Column value from DataGridRow in WPF从 WPF 中的 DataGridRow 获取列值
【发布时间】:2013-01-02 01:36:32
【问题描述】:
我正在创建一个 WPF 应用程序,在该应用程序中,当用户单击 DataGrid 的一行时,我需要获取一个 Column 值并使用该值从数据库中获取数据。
我能够找到 DataGridRow 但无法获取列值。这是我的代码...
DataGridRow BillRow = sender as DataGridRow;
我将所选行的详细信息放入 BillRow(我可以在 Visualiser 中看到它们),但无法将值放入变量中。你能帮助我吗 ??
【问题讨论】:
标签:
c#
wpf
wpf-controls
wpfdatagrid
【解决方案1】:
以下解决方案可能对您有所帮助
public static DataGridCell GetCell(DataGrid dataGrid, int row, int column)
{
DataGridRow rowContainer = GetRow(dataGrid, row);
if (rowContainer != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
// try to get the cell but it may possibly be virtualized
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
if (cell == null)
{
// now try to bring into view and retreive the cell
dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[column]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
}
return cell;
}
return null;
}