在运行时添加和删除 WPF UIElement
有没有一种方法可以对 UIElements(例如运行时添加的形状和控件)进行逻辑分组或标记以便于删除?
例如,我有一个带有一些(设计时)子元素的 Grid ,并在运行时向其中添加省略号和 TextBlock 。当我想绘制一组不同的椭圆和 TextBlock
时,我想删除添加的原始组。在添加它们时对它们进行逻辑分组的简单方法是什么,这样我就可以使用children.clear()或某种方法来识别它们以将其删除?
可以添加标签值,但在迭代控件的子级时无法检索或读取该值,因为它们的类型为 UIElement
,没有标签属性。
想法?
Is there a way to logically group or tag UIElements like shapes and controls added at runtime for easy removal?
For eg., I have a Grid
with some (design-time) child elements and add Ellipses and TextBlock
s to it at runtime. When I want to draw a different set of Ellipses and TextBlock
s, I'd like to remove the original set I added. What would be an easy way to logically group these while adding them so I can just have a children.clear() or some way to identify them to remove them?
It is possible to add a tag value but there is no way to retrieve or read this while iterating through children of a control because they are of type UIElement
which does not have a tag property.
Thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个使用
附加属性
的好地方。示例:
用法:
A very good place to use an
Attached Property
.Example:
Usage:
尝试在网格内的
Canvas
上绘图...这样就很简单:希望它有所帮助。
Try drawing on a
Canvas
inside your grid... that way its as easy as:Hope it helps.