如何获取 WPF 用户控件可见部分的大小?
我有一个由标签和文本框组成的用户控件。它位于滚动查看器内。
我正在其顶部绘制一个装饰器,我需要将装饰器的大小调整为控件的可见大小。
如何获得控件的可见大小?
在下图中,绿色矩形是装饰器。正如您所看到的,它被绘制在右侧的滚动条上。
是否可以获得渲染部分的大小,或者我必须手动遍历可视化树并计算它?
编辑:更多背景信息
我正在构建一个非常有限的表单设计器。一切都在代码中发生。装饰器用于显示当前选定的控件。
I have a usercontrol that consists of a label and a textbox. It's inside a scrollviewer.
I'm drawing an adorner on top of it and I need to adjust the size of the adorner to the visible size of the control.
How do I get the visible size of the control?
In the image below the green rectangle is the adorner. As you can see it's being drawn over the scrollbar on the right side.
Is it possible to get the size of the rendered part or will I have to manually go trough the visual tree and calculate it?
edit: more background info
I'm build a very limited form designer. Everything is happening in code. The adorner is used to display the current selected control.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会在你的 ScrollViewer 中放置一个 Canvas 并将所有用户控件放置在画布上。如果随后将装饰器绘制在画布上,您不必担心它会绘制在 ScrollViewer 之上。
您还可以获得额外的好处,即装饰器消失在 ScrollViewer 下,而不是仅仅结束于它,因此您的用户知道控件超出了 ScrollViewer 的范围。这就是我在 WPF 中制作和见过的所有设计器的工作方式。
I would put a Canvas in your ScrollViewer and place all of your user controls on the Canvas. If the Adorner is then painted on the Canvas you don't have to worry about it drawing over top your ScrollViewer.
You would also have the added benefit of the adorner disappearing under the ScrollViewer, rather than just ending at it, so your users know that the control extends beyond the ScrollViewer. This is how all of the designers I've made and seen made in WPF work.
如果您只担心修剪装饰器,那么您可以包含 AdornerDecorator 在您的内容中。例如:
或者您可以将 AdornerDecorator 直接包含在您的 UserControl 中。
If you are only worried about clipping the adorner, then you can include an AdornerDecorator in your content. Something like:
Or you can include the AdornerDecorator directly in your UserControl.