FindParent<T>使用 WPFToolkit:Datagrid 时为空 [英] FindParent&lt;T&gt; is null while using WPFToolkit:Datagrid

查看:28
本文介绍了FindParent<T>使用 WPFToolkit:Datagrid 时为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 xaml 代码时.

When I am using xaml code.

<DataGrid Name="DataGrid1" 
          ItemsSource="{Binding Path=MainSearchBinding}"  
          HorizontalScrollBarVisibility="Hidden" SelectionMode="Extended"
          CanUserAddRows="False" CanUserDeleteRows="False"
          CanUserResizeRows="False" CanUserSortColumns="True"
          AutoGenerateColumns="False" IsTextSearchEnabled="True" IsReadOnly="True" 
          RowHeaderWidth="17" SelectionChanged="DataGrid1_SelectionChanged"
          MouseDoubleClick="OnDoubleClick" MouseLeftButtonUp="OnMouseClick">

它工作正常.
切换到 :

It is working fine.
While switching to <WpfToolkit:Datagrid></WpfToolkit:Datagrid>:

<WpfToolkit:DataGrid Name="DataGrid1" 
                     ItemsSource="{Binding Path=MainSearchBinding}"  
                     HorizontalScrollBarVisibility="Hidden" SelectionMode="Extended"
                     CanUserAddRows="False" CanUserDeleteRows="False"
                     CanUserResizeRows="False" CanUserSortColumns="True"
                     AutoGenerateColumns="False" IsTextSearchEnabled="True" IsReadOnly="True" 
                     RowHeaderWidth="17" SelectionChanged="DataGrid1_SelectionChanged"
                     MouseDoubleClick="OnDoubleClick" MouseLeftButtonUp="OnMouseClick">

我有错误:

值不能为空.参数名称:元素"

"Value cannot be null. Parameter name: element"

在这一行中使用 FindParent(...):

DependencyObject parentObject = VisualTreeHelper.GetParent(child);

public static T FindParent<T>(this DependencyObject child)
        where T : DependencyObject
{
    //get parent item
    DependencyObject parentObject = VisualTreeHelper.GetParent(child);

    //we've reached the end of the tree
    if (parentObject == null) return null;

    //check if the parent matches the type we're looking for
    var parent = parentObject as T;
    if (parent != null)
    {
        return parent;
    }
    else
    {
        return FindParent<T>(parentObject);
    }
}

我背后的代码在这里.如果您单击数据网格单元格,它将打开新选项卡.

My code behind is here. It is opening new tab if you click on datagrid cells.

var tabControl = (sender as DataGrid).FindParent<TabControl>();
tabControl.Items.Add(new TabItem() { Header = "Документ", Content = docview, IsSelected = true });

我知道我遗漏了一些东西,请告诉我该往哪里移动?提前致谢.

I know that I am missing something, please show me where to move? Thanks in advance.

推荐答案

问题出在这里:

var tabControl = (sender as DataGrid).FindParent<TabControl>();

WPF 工具包 DataGrid 具有类 Microsoft.Windows.Controls.DataGrid,而内置 WPF DataGrid 具有类 System.Windows.Controls.DataGrid.如果您的 sender 对象是 WPF Toolkit DataGrid,并且上面代码行中的 DataGrid 是内置的 WPF DataGrid,则 sender as DataGrid 将为空.WPF 工具包 DataGrid 与内置的 DataGrid 完全分开,特别是不继承自它.

The WPF Toolkit DataGrid has class Microsoft.Windows.Controls.DataGrid, whereas the built-in WPF DataGrid has class System.Windows.Controls.DataGrid. If your sender object is a WPF Toolkit DataGrid, and the DataGrid in the line of code above is the built-in WPF DataGrid, then sender as DataGrid will be null. The WPF Toolkit DataGrid is completely separate to the built-in DataGrid and in particular does not inherit from it.

幸运的是,这个问题很容易解决.您不需要将 sender 转换为任一 DataGrid 类.您的 FindParent 扩展方法适用于 DependencyObject,并且两个 DataGrid 类都继承自 DependencyObject,因此您可以写

Fortunately, this problem is easy to fix. You don't need to cast sender to either DataGrid class. Your FindParent<T> extension method works on DependencyObjects, and both DataGrid classes inherit from DependencyObject, so you can write

var tabControl = (sender as DependencyObject).FindParent<TabControl>();

相反.

这篇关于FindParent<T>使用 WPFToolkit:Datagrid 时为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆