Can I access ItemsHost of ItemsControl using refle

2019-02-18 01:14发布

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条回答
来,给爷笑一个
2楼-- · 2019-02-18 01:53

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.

查看更多
登录 后发表回答