从DataGrid内部的CheckBox获取包含行

从DataGrid内部的CheckBox获取包含行

本文介绍了从DataGrid内部的CheckBox获取包含行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有自动生成的列的DataGrid。在AutoColumnsGenerated事件的旁边,我要添加一个附加列。此列是一个DataGridTemplateColumn,其中包含一个VisualTemplate设置为CheckBox的DataTemplate。我要在CheckBox.ClickEvent中添加一个处理程序,在其中将在相关行中链接一个值。

I have a DataGrid that has auto generated columns. In side of the AutoColumnsGenerated event I am adding an additional column. This column is a DataGridTemplateColumn which includes a DataTemplate with it's VisualTree set to a CheckBox. I am adding a handler to the CheckBox.ClickEvent in which I will chain a value in the associated row.

如果我有多个以此方式构建的DataGrid,我不会我不知道如何确定点击事件源自哪个列表视图。

If I have multiple DataGrids that are built this way I don't know how to figure out which listview the click event originated from.

从点击事件处理程序中,我可以访问复选框,但未设置其父级。我也尝试过使用可视T恤助手,但无法在正确的位置进入树中。

From the click event handler I have access to the check box, but its parent is not set. I have also tried using the visual tee helper, but cannot get into the tree in the correct spot.

有人知道我如何找出包含单击复选框?

Does anybody know how I can find out the corresponding DataGrid that contains the clicked CheckBox?

推荐答案

VisualTreeHelper应该可以工作。在事件处理程序上尝试以下代码:

VisualTreeHelper should work. Try the following code on the event handler:

FrameworkElement fe = sender as FrameworkElement;

while ((fe.GetType() != typeof(DataGrid)) &&
       (fe != null))
{
     fe = VisualTreeHelper.GetParent(fe) as FrameworkElement;
}

这篇关于从DataGrid内部的CheckBox获取包含行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 21:57