如何获取 WPF 用户控件可见部分的大小?

发布于 2024-11-09 02:57:50 字数 329 浏览 0 评论 0原文

我有一个由标签和文本框组成的用户控件。它位于滚动查看器内。

我正在其顶部绘制一个装饰器,我需要将装饰器的大小调整为控件的可见大小。

如何获得控件的可见大小?

在下图中,绿色矩形是装饰器。正如您所看到的,它被绘制在右侧的滚动条上。

是否可以获得渲染部分的大小,或者我必须手动遍历可视化树并计算它?

编辑:更多背景信息

我正在构建一个非常有限的表单设计器。一切都在代码中发生。装饰器用于显示当前选定的控件。

样本

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.

sample

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

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

发布评论

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

评论(2

我们只是彼此的过ke 2024-11-16 02:57:50

我会在你的 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.

半透明的墙 2024-11-16 02:57:50

如果您只担心修剪装饰器,那么您可以包含 AdornerDecorator 在您的内容中。例如:

<ScrollViewer>
    <AdornerDecorator>
        <!-- Your content here -->
    </AdornerDecorator>
</ScrollViewer>

或者您可以将 AdornerDecorator 直接包含在您的 UserControl 中。

If you are only worried about clipping the adorner, then you can include an AdornerDecorator in your content. Something like:

<ScrollViewer>
    <AdornerDecorator>
        <!-- Your content here -->
    </AdornerDecorator>
</ScrollViewer>

Or you can include the AdornerDecorator directly in your UserControl.

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