Can I access ItemsHost of ItemsControl using reflection?

删除回忆录丶 提交于 2019-12-30 09:58:32

问题


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 ?


回答1:


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);
    }
}

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.



来源:https://stackoverflow.com/questions/5056728/can-i-access-itemshost-of-itemscontrol-using-reflection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!