此示例演示如何重写 OnRender 方法 Panel ,以便向布局元素添加自定义图形效果。
示例:
OnRender使用该方法向呈现的面板元素添加图形效果。 例如,可以使用此方法添加自定义边框或背景效果。 对象 DrawingContext 作为参数传递,该参数提供用于绘制形状、文本、图像或视频的方法。 因此,此方法可用于自定义面板对象。
// Override the OnRender call to add a Background and Border to the OffSetPanel
protected override void OnRender(DrawingContext dc)
{
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = Colors.LimeGreen;
Pen myPen = new Pen(Brushes.Blue, 10);
Rect myRect = new Rect(0, 0, 500, 500);
dc.DrawRectangle(mySolidColorBrush, myPen, myRect);
}
' Override the OnRender call to add a Background and Border to the OffSetPanel
Protected Overrides Sub OnRender(ByVal dc As DrawingContext)
Dim mySolidColorBrush As New SolidColorBrush()
mySolidColorBrush.Color = Colors.LimeGreen
Dim myPen As New Pen(Brushes.Blue, 10)
Dim myRect As New Rect(0, 0, 500, 500)
dc.DrawRectangle(mySolidColorBrush, myPen, myRect)
End Sub