How do I get the size of the visible part of a WPF usercontrol?

一曲冷凌霜 提交于 2019-12-12 13:13:41

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/6112533/how-do-i-get-the-size-of-the-visible-part-of-a-wpf-usercontrol

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!