WPF Adorner Clipping
Asked Answered
N

3

10

I have an ItemsControl in a ScrollViewer. The items in the ItemsControl are expanded to a DataTemplate which basically consists of an Adorner.

Now the problem is, when scrolling, the Visual Children of the Adorner are visible outside the ScrollViewer. Lets say I scroll from the Horizontal Offset 0 to 100, the Visual Children of the Adorner move to the left and are visible next to the ScrollViewer, although they should be hidden. Setting ClipToBounds on the ItemsControl or the ScrollViewer does not work.

I understand, that Adorner are rendered above all elements z-order wise, but they really shouldn't be visible in such cases as with the ScrollViewer. The adorned Element by the way behaves like expected and is not visible through the ScrollViewer.

Is there any easy way to "clip" the Adorners, so that they are only visible in the visible scroll area?

Thanks, Andrej

Neck answered 3/9, 2009 at 10:9 Comment(2)
In the following thread Wei Zhou re-templates the ScrollViewer so the button's Adorner is constrained. Implement selection highlighting - advice pleaseDispeople
What version of .NET/WPF did this happen for you? I'm actually trying to recreate this behavior and I can't. Basically I want my error adorners to display past the bounds of the scrollviewer (without using a popup)Aswan
P
11

Setting ClipToBounds on the containing control is not enough. You must set the adorner's IsClipEnabled property too.

Planer answered 8/12, 2015 at 14:5 Comment(1)
This was best solution for me.Elbertina
U
2

I've encountered the same problem when subclassing the WPFToolkit DataGrid to draw an adorner around the current cell.

The content of the ScrollViewer is rendered by a ScrollContentPresenter instance. ScrollContentPresenter has its own adorner layer, which is accessible through the ScrollContentPresenter.AdornerLayer property.

I found that my adorner correctly clips if I add it to that layer.

Unaffected answered 26/10, 2009 at 5:33 Comment(1)
I use VisualTreeHelper to search the visual tree until I find a descendant of type ScrollContentPresenter. Take a look at the method in this thread - wpf.codeplex.com/Thread/View.aspx?ThreadId=34542 .Unaffected
R
1

My solution was to push a clip region onto the drawing context, render whatever I needed, and pop the clipping at the end, like this:

drawingContext.PushClip(new RectangleGeometry(new Rect(0, 0, this.AdornedElement.RenderSize.Width, this.AdornedElement.RenderSize.Height)));
// continue drawing
drawingContext.Pop();

You can plug this in into any Adorner, the bounds are already available as part of the element.

Rowboat answered 29/1, 2018 at 20:53 Comment(1)
If you're rending into a text box or something with a scroll viewer, you can use the ViewportWidth and ViewportHeight instead of RenderSize. You may need to add 4 to the ViewportWidth for text boxes though, because there's some weird paddingPumpkinseed

© 2022 - 2025 — McMap. All rights reserved.