WPF 装饰器

首先 准备一个装饰器类 继承Adorner

 1 /// <summary>
 2     /// 装饰器必须继承抽象类Adorner
 3     /// </summary>
 4     public class CornerAdorner : Adorner
 5     {
 6         /// <summary>
 7         /// 调用基类构造函数
 8         /// </summary>
 9         /// <param name="element"></param>
10         public CornerAdorner(UIElement element) : base(element)
11         {
12         }
13 
14         /// <summary>
15         /// 重写绘制方法
16         /// </summary>
17         /// <param name="drawingContext"></param>
18         protected override void OnRender(DrawingContext drawingContext)
19         {
20             Rect rect = new Rect(this.AdornedElement.RenderSize);//装饰器所装饰的元素最终大小
21 
22             Pen pen = new Pen(new SolidColorBrush(Colors.OrangeRed), 1);
23 
24             drawingContext.DrawLine(pen, new Point(rect.TopLeft.X - 3, rect.TopLeft.Y - 3), new Point(rect.TopLeft.X + 5, rect.TopLeft.Y - 3));
25             drawingContext.DrawLine(pen, new Point(rect.TopLeft.X - 3, rect.TopLeft.Y - 3), new Point(rect.TopLeft.X - 3, rect.TopLeft.Y + 5));
26 
27             drawingContext.DrawLine(pen, new Point(rect.TopRight.X + 3, rect.TopRight.Y - 3), new Point(rect.TopRight.X - 5, rect.TopRight.Y - 3));
28             drawingContext.DrawLine(pen, new Point(rect.TopRight.X + 3, rect.TopRight.Y - 3), new Point(rect.TopRight.X + 3, rect.TopRight.Y + 5));
29 
30             drawingContext.DrawLine(pen, new Point(rect.BottomLeft.X - 3, rect.BottomLeft.Y + 3), new Point(rect.BottomLeft.X + 5, rect.BottomLeft.Y + 3));
31             drawingContext.DrawLine(pen, new Point(rect.BottomLeft.X - 3, rect.BottomLeft.Y + 3), new Point(rect.BottomLeft.X - 3, rect.BottomLeft.Y - 5));
32 
33             drawingContext.DrawLine(pen, new Point(rect.BottomRight.X + 3, rect.BottomRight.Y + 3), new Point(rect.BottomRight.X - 5, rect.BottomRight.Y + 3));
34             drawingContext.DrawLine(pen, new Point(rect.BottomRight.X + 3, rect.BottomRight.Y + 3), new Point(rect.BottomRight.X + 3, rect.BottomRight.Y - 5));
35         }
36     }

界面

 1 <Grid>
 2         <StackPanel x:Name="stackPanel"
 3             HorizontalAlignment="Center"
 4             VerticalAlignment="Center"
 5             Orientation="Vertical">
 6             <TextBox Width="200" Margin="15" />
 7             <TextBox Width="200" Margin="15" />
 8             <TextBox Width="200" Margin="15" />
 9             <TextBlock
10                 Width="200"
11                 Margin="15"
12                 HorizontalAlignment="Center"
13                 Text="边框测试" />
14             <Button Margin="15" Content="添加边框" Click="Button_Click" />
15             <Button Margin="15" Content="移除边框" Click="Button_Click_1" />
16         </StackPanel>
17     </Grid>

点击事件

 1 /// <summary>
 2         /// 添加装饰器
 3         /// </summary>
 4         /// <param name="sender"></param>
 5         /// <param name="e"></param>
 6         private void Button_Click(object sender, RoutedEventArgs e)
 7         {
 8             foreach (UIElement element in stackPanel.Children)
 9             {
10                 if (element is TextBox || element is TextBlock)
11                 {
12                     var layer = AdornerLayer.GetAdornerLayer(element);//获取元素的装饰器层
13                     layer.Add(new CornerAdorner(element));//把装饰器添加到层
14                 }
15             }
16         }
17 
18         /// <summary>
19         /// 移除装饰器
20         /// </summary>
21         /// <param name="sender"></param>
22         /// <param name="e"></param>
23         private void Button_Click_1(object sender, RoutedEventArgs e)
24         {
25             foreach (UIElement element in stackPanel.Children)
26             {
27                 var layer = AdornerLayer.GetAdornerLayer(element);
28                 var adorners = layer.GetAdorners(element);//获取控件上面的所有装饰器
29                 if (adorners != null)
30                 {
31                     for (int i = adorners.Length - 1; i >= 0; i--)
32                     {
33                         Adorner adorner = adorners[i];
34                         layer.Remove(adorner);//移除装饰器
35                     }
36                 }
37             }
38         }

效果

WPF 装饰器

如果要实现点击才出现这个边框,可参考此内容 

 

WPF使用装饰器实现SAP焦点样式

 WPF使用装饰器实现SAP焦点样式_林老师的专栏-CSDN博客_wpf 装饰器

上一篇:面试题-python3 找出列表中出现的所有连续数字


下一篇:从1到100的自然数中,每次取出两个数,要使它们的和大于100,共有哪些取法,总计多少种取法?