2

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?

JBSnorro
  • 6,048
  • 3
  • 41
  • 62
  • What are you trying to do? overriding `OnRender()` is not something that you would do regularily in WPF. – Federico Berasategui Jan 08 '14 at 17:08
  • Despite the fact that it would be difficult to call the protected AddVisualChild method on the Grid instance. – Clemens Jan 08 '14 at 17:52
  • @HighCore: I'm trying to draw a few lines on top of the grid. I thought I'd bring them together in an element that specializes in drawing those lines. I'm new to WPF btw, as you may well note... – JBSnorro Jan 08 '14 at 18:54
  • @JBSnorro you may want to look at [My Example](http://stackoverflow.com/questions/15469283/how-does-one-animate-a-line-on-a-canvas-in-c/15469477#15469477) of such a thing in WPF using proper WPF patterns and practices. – Federico Berasategui Jan 08 '14 at 18:56

1 Answers1

4

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.

Just TFS
  • 4,697
  • 1
  • 18
  • 22
dev hedgehog
  • 8,698
  • 3
  • 28
  • 55