我可以使用反射访问 ItemsControl 的 ItemsHost 吗? [英] Can I access ItemsHost of ItemsControl using reflection?

查看:27
本文介绍了我可以使用反射访问 ItemsControl 的 ItemsHost 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建从 DataGrid 派生的自定义 ItemsControl.我需要访问 ItemsHost,它是实际保存 DataGrid 行的 Panel.我见过一些丑陋的技巧来做到这一点,但我认为它们比使用反射更糟糕.那么我可以使用反射访问 ItemsHost 吗?以及如何?

I'm creating custom ItemsControl that is derived from DataGrid. I need to access ItemsHost that is the Panel that actually holds rows of DataGrid. I have seen som ugly tricks to do that but I consider them worse then using reflection. So can I access ItemsHost using reflection ? And how ?

推荐答案

是的,我可以.这很简单 - 我刚刚在继承自 DataGrid 的类中创建了属性:

Yes I can. It is simple - I've just created property in class inheriting from DataGrid:

protected Panel ItemsHost {
    get {
        return (Panel) typeof (MultiSelector).InvokeMember("ItemsHost",
            BindingFlags.NonPublic | BindingFlags.GetProperty | BindingFlags.Instance,
            null, this, null);
    }
}

它就像一个魅力:).我可以获取 ItemsControl 类的 ItemsHost 内部属性的值.这样我就可以访问任何不受保护的属性.

It works like a charm :). I can get the value of ItemsHost internal property of the ItemsControl class. This way I can access any non-protected properties.

这篇关于我可以使用反射访问 ItemsControl 的 ItemsHost 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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