简体   繁体   English

WPF-视觉树不用于绘图吗?

[英]WPF - Visual tree not used for drawing?

I am having troubles understanding when the OnRender method is invoked. 我很难理解何时调用OnRender方法。 Consider my example: I have a class SomeElement deriving from FrameworkElement which overrides the OnRender method. 考虑我的示例:我有一个继承自FrameworkElement的SomeElement类,该类重写OnRender方法。 Suppose I have a Grid grid . 假设我有一个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. 会导致SomeElement.OnRender方法被触发。 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. 所以我的问题是,为什么仅将someElement添加到可视化树中却没有绘制它。 What is the importance of adding it to the property Children ? 将其添加到Children属性中有什么重要性? And more generally, what how is OnRender called? 更一般地说,OnRender怎么称呼? Via the visual tree, or Children property, or? 通过视觉树,还是Children属性,还是?

AddVisualChild is not doing what you might be thinking is does. AddVisualChild没有做您可能想做的事情。

AddVisualChild is just there to create a child-parent relationship between two visuals in VisualTree. AddVisualChild就是在那里在VisualTree中的两个视觉对象之间创建子对父关系。 Nothing more or less. 或多或少。 VisualTree doesn't render everything that is inside it. VisualTree不会渲染其中的所有内容。

AddLogicalChild does the same just it creates LogicalTree. AddLogicalChild所做的只是创建LogicalTree。 LogicalTree is usually smaller and shorter than VisualTree, which means its faster. LogicalTree通常比VisualTree小和短,这意味着它更快。

In order to fully support rendering of your children, you need to call measure method on them and arrange method. 为了完全支持孩子的渲染,您需要在孩子身上调用measure方法并安排方法。 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. 此外,您需要更改父级的VisualChildrenCount属性并将正确的尺寸传递给每个孩子,并且需要将它们放置在适当的位置,以便用户能够看到它们。 Then things will get rendered. 然后,事物将被渲染。

Calling AddVisualChild alone and InvalidateVisual is not doing anything. 单独调用AddVisualChild和InvalidateVisual不会执行任何操作。

Children collection, on the other hand, is a UIElementCollection that does all the above things I mentioned automatically. 另一方面,Children集合是一个UIElementCollection,它可以自动执行上述所有操作。 That is why it working with Children.Add(...). 这就是为什么它与Children.Add(...)一起工作的原因。

Futhermore, you should always be working with Children collection instead of writing your own things. 此外,您应该始终使用Children系列,而不是编写自己的东西。

Like HighCore mentioned many problems can be solved by simply having a proper MVVM. 像提到的HighCore一样,只需拥有适当的MVVM即可解决许多问题。

Maybe you do not need to have a custom control. 也许您不需要自定义控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM