WPF 工具包 DataGrid SelectionChanged 获取单元格值 [英] WPF Toolkit DataGrid SelectionChanged Getting Cell Value
本文介绍了WPF 工具包 DataGrid SelectionChanged 获取单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请帮助我,我正在尝试从 SelectionChangedEvent 中的选定行获取 Cell[0] 的值.
Please help me, Im trying to get the value of Cell[0] from the selected row in a SelectionChangedEvent.
我只是设法获得了许多不同的 Microsoft.Windows.Controls,我希望我错过了一些愚蠢的东西.
I am only managing to get lots of different Microsoft.Windows.Controls and am hoping im missing something daft.
希望我能从这里得到一些帮助...
Hoping I can get some help from here...
private void datagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Microsoft.Windows.Controls.DataGrid _DataGrid = sender as Microsoft.Windows.Controls.DataGrid;
}
我希望它会像......
I was hoping it would be something like...
_DataGrid.SelectedCells[0].Value;
然而 .Value 不是一个选项......
However .Value isn't an option....
非常感谢这让我发疯了!丹
Many many thanks this has been driving me mad! Dan
推荐答案
请检查以下代码是否适合您:
pls, check if code below would work for you:
private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DataGrid dataGrid = sender as DataGrid;
if (e.AddedItems!=null && e.AddedItems.Count>0)
{
// find row for the first selected item
DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(e.AddedItems[0]);
if (row != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
// find grid cell object for the cell with index 0
DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(0) as DataGridCell;
if (cell != null)
{
Console.WriteLine(((TextBlock)cell.Content).Text);
}
}
}
}
static T GetVisualChild<T>(Visual parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null) child = GetVisualChild<T>(v);
if (child != null) break;
}
return child;
}
希望能帮到你,问候
这篇关于WPF 工具包 DataGrid SelectionChanged 获取单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文