问题
I am having troubles understanding when the OnRender method is invoked.
Consider my example:
I have a class SomeElement deriving from FrameworkElement which overrides the OnRender method.
Suppose I have a Grid grid
. What I would expect is that
var someElement = new SomeElement();
grid.AddVisualChild(someElement);
someElement.InvalidateVisual();
would cause the SomeElement.OnRender method to be triggered. It doesn't in my case, but the following does:
var someElement = new SomeElement();
grid.Children.Add(new SomeElement());
someElement.InvalidateVisual();
So my question is why someElement
isn't drawn when it's added solely to the visual tree. What is the importance of adding it to the property Children
?
And more generally, what how is OnRender called? Via the visual tree, or Children
property, or?
回答1:
AddVisualChild is not doing what you might be thinking is does.
AddVisualChild is just there to create a child-parent relationship between two visuals in VisualTree. Nothing more or less. VisualTree doesn't render everything that is inside it.
AddLogicalChild does the same just it creates LogicalTree. LogicalTree is usually smaller and shorter than VisualTree, which means its faster.
In order to fully support rendering of your children, you need to call measure method on them and arrange method. Futhermore, you need to change the VisualChildrenCount property of your parent and to pass the right size to each child and you need to place them on proper position for user to be able to see them. Then things will get rendered.
Calling AddVisualChild alone and InvalidateVisual is not doing anything.
Children collection, on the other hand, is a UIElementCollection that does all the above things I mentioned automatically. That is why it working with Children.Add(...).
Futhermore, you should always be working with Children collection instead of writing your own things.
Like HighCore mentioned many problems can be solved by simply having a proper MVVM.
Maybe you do not need to have a custom control.
来源:https://stackoverflow.com/questions/21001959/wpf-visual-tree-not-used-for-drawing