在WPF中使用AddLogicalChild、AddVisualChild添加UIElement

发布于 2024-11-14 23:08:21 字数 454 浏览 4 评论 0原文

我通过简单地从面板扩展/继承来创建画布的自定义版本。

当我想在其上绘制或渲染某些内容时,我只需创建一个 DrawingVisual,绘制所需的图形并调用 AddLogicalChild(Visual)AddVisualChild(Visual) 并增加面板视觉效果的计数。

当我添加 DrawingVisual 实例时,这非常有用,但是当我尝试在此处添加 Button 并定义尺寸时(MinHeight, MinWidth),则不显示。

UIElements 是否可能需要不同的处理逻辑才能显示?基本上,如何将 UIElement 添加到将显示并可以操作的扩展 Panel 中?

I created a custom version of canvas by simply extending/inheriting from the Panel.

When I want to draw or render something on it, I simply create a DrawingVisual, draw the desired graphics and call the AddLogicalChild(Visual), AddVisualChild(Visual) and incrementing the count of the Visuals of the Panel.

This works great when I am adding DrawingVisual instances, but when I try to add a Button here and define the dimensions (MinHeight, MinWidth), it is not displayed.

Is it possible that UIElements need a different logic of handling to be displayed? Basically, how can I add a UIElement to that extended Panel that would be displayed and could be manipulated with?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

扎心 2024-11-21 23:08:21

如果仅使用 UIElements,则首选方法是将这些项添加到 Children 集合中。

但是如果您混合使用UIElementVisual,我建议使用方法AddLogicalChild / AddVisualChild 而不是 Children。但是,这需要更多的工作:

  1. 使用 AddLogicalChild 将项目(Visual 以及 UIElement)添加为逻辑和可视子项> 和AddVisualChild
  2. 将项目存储在某处(面板不会为您执行此操作)
  3. 覆盖 GetVisualChildVisualChildrenCount 并返回项目数/传递索引上的项目
  4. 覆盖 MeasureOverride,调用 UIElement.Measure 在每个 UIElement 上,并执行特定于您的面板的所有其他测量内容
  5. 覆盖 ArrangeOverride,为每个 UIElement.Arrange 调用UIElement 并执行特定于您的面板的所有其他排列操作

请注意,MeasureArrange 必须在 after UIElement 已添加到 Panel 中。

就是这样。 :)

If working only with UIElements, the prefered way is to add that items to the Children collection.

But if you have a mixture of UIElements and Visuals, I would suggest to use the methods AddLogicalChild / AddVisualChild instead of Children. However, that requires a little bit more work:

  1. Add the item (Visuals as well as UIElements) as logical and visual children, using AddLogicalChild and AddVisualChild.
  2. Store the items somewhere (the Panel wont do it for you)
  3. Overwrite GetVisualChild and VisualChildrenCount and return the number of items / the item on the passed index
  4. Overwrite MeasureOverride, call UIElement.Measure on each UIElement and do all the other measure stuff that is specific for your panel
  5. Overwrite ArrangeOverride, call UIElement.Arrange for each UIElement and do all the other arrange stuff that is specific for your panel

Note that Measure and Arrange have to be called after the UIElement has been added to the Panel.

Thats it. :)

眼眸里的快感 2024-11-21 23:08:21

该功能已经包含在内。如果您想将 UIElements 添加到您的 Panel,只需使用 .Children.Add() - 就像使用常规 一样CanvasGrid 等。

然后在您的实现中,覆盖 MeasureOverrideArrangeOverride 以迭代子级并将它们组织在您的 <代码>面板表面。

示例如下: http://msdn.microsoft .com/en-us/library/ms754152(v=vs.110).aspx#Panels_custom_panel_elements

That functionality is already included. If you want to add UIElements to your Panel, simply use .Children.Add() - just like you do with the regular Canvas, Grid etc.

Then in your implementation, override MeasureOverride and ArrangeOverride to iterate through the children and organize them on your Panel surface.

Example here: http://msdn.microsoft.com/en-us/library/ms754152(v=vs.110).aspx#Panels_custom_panel_elements

走走停停 2024-11-21 23:08:21

对于 UIElement 来说,它是要使用的 Collection Children

public void AddChild(UIElement newChild)
{
   this.Children.Add(newChild);
}

您还应该查看 InternalChildren 集合,建议在 Panel 的扩展中使用。

For UIElements, it's the Collection Children to use:

public void AddChild(UIElement newChild)
{
   this.Children.Add(newChild);
}

You should also look at the InternalChildren Collection which is recommanded to use in extensions of Panels.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文