One way is to override the OnRender
of the UIElement
as seen in the MSDN SimpleCircleAdorner example.
// A common way to implement an adorner's rendering behavior is to override the OnRender
// method, which is called by the layout system as part of a rendering pass.
protected override void OnRender(DrawingContext drawingContext)
{
Rect adornedElementRect = new Rect(this.AdornedElement.DesiredSize);
// Some arbitrary drawing implements.
SolidColorBrush renderBrush = new SolidColorBrush(Colors.Green);
renderBrush.Opacity = 0.2;
Pen renderPen = new Pen(new SolidColorBrush(Colors.Navy), 1.5);
double renderRadius = 5.0;
// Draw a circle at each corner.
drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.TopLeft, renderRadius, renderRadius);
drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.TopRight, renderRadius, renderRadius);
drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.BottomLeft, renderRadius, renderRadius);
drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.BottomRight, renderRadius, renderRadius);
}
}
If you want to provide a robust solution outside a typical rectangle or rounded rectangle you will have to make use of path geometries which will allow you to build out a path composed of segments such as a BezierSegment
, a LineSegment
, or an ArcSegment
thus creating an appropriate path around the UIElement
.
If on the other hand a rectangle or rounded rectangle would suffice you can make use of the DrawingContext.DrawRectangle
and DrawingContext.DrawRoundedRectangle
respectively within the OnRender
override.