WPF Adorner Clipping

独自空忆成欢 提交于 2019-12-04 04:16:43

In the following thread Wei Zhou re-templates the ScrollViewer so the button's Adorner is constrained.

Implement selection highlighting - advice please

Regards David

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

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.

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.

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