在WPF中使用AddLogicalChild、AddVisualChild添加UIElement
我通过简单地从面板扩展/继承来创建画布的自定义版本。
当我想在其上绘制或渲染某些内容时,我只需创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果仅使用
UIElement
s,则首选方法是将这些项添加到Children
集合中。但是如果您混合使用
UIElement
和Visual
,我建议使用方法AddLogicalChild
/AddVisualChild
而不是Children
。但是,这需要更多的工作:AddLogicalChild
将项目(Visual
以及UIElement
)添加为逻辑和可视子项> 和AddVisualChild
。GetVisualChild
和VisualChildrenCount
并返回项目数/传递索引上的项目UIElement.Measure
在每个UIElement
上,并执行特定于您的面板的所有其他测量内容UIElement.Arrange
调用UIElement
并执行特定于您的面板的所有其他排列操作请注意,
Measure
和Arrange
必须在 after UIElement 已添加到Panel
中。就是这样。 :)
If working only with
UIElement
s, the prefered way is to add that items to theChildren
collection.But if you have a mixture of
UIElement
s andVisual
s, I would suggest to use the methodsAddLogicalChild
/AddVisualChild
instead ofChildren
. However, that requires a little bit more work:Visual
s as well asUIElement
s) as logical and visual children, usingAddLogicalChild
andAddVisualChild
.GetVisualChild
andVisualChildrenCount
and return the number of items / the item on the passed indexUIElement.Measure
on eachUIElement
and do all the other measure stuff that is specific for your panelUIElement.Arrange
for eachUIElement
and do all the other arrange stuff that is specific for your panelNote that
Measure
andArrange
have to be called after the UIElement has been added to thePanel
.Thats it. :)
该功能已经包含在内。如果您想将
UIElements
添加到您的Panel
,只需使用.Children.Add()
- 就像使用常规一样Canvas
、Grid
等。然后在您的实现中,覆盖
MeasureOverride
和ArrangeOverride
以迭代子级并将它们组织在您的 <代码>面板表面。示例如下: 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 yourPanel
, simply use.Children.Add()
- just like you do with the regularCanvas
,Grid
etc.Then in your implementation, override
MeasureOverride
andArrangeOverride
to iterate through the children and organize them on yourPanel
surface.Example here: http://msdn.microsoft.com/en-us/library/ms754152(v=vs.110).aspx#Panels_custom_panel_elements
对于
UIElement
来说,它是要使用的 CollectionChildren
:您还应该查看 InternalChildren 集合,建议在
Panel
的扩展中使用。For
UIElement
s, it's the CollectionChildren
to use:You should also look at the InternalChildren Collection which is recommanded to use in extensions of
Panel
s.