【问题标题】:Get containing row from a CheckBox inside of a DataGrid从 DataGrid 内的 CheckBox 获取包含行
【发布时间】:2011-04-02 22:18:39
【问题描述】:

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

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

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

有人知道我怎样才能找到包含点击的复选框的相应 DataGrid 吗?

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢!我循环的是 DependencyObjects 而不是 FrameworkElements。
    猜你喜欢
    • 2015-06-12
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    • 2017-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多