Adorner | 一个抽象基类,所有具体装饰器的实现都从该类继承。 |
AdornerLayer | 一个类,表示一个或多个装饰元素的装饰器的呈现层。 |
AdornerDecorator | 一个类,使装饰器层与元素集合相关联。 |
public class FocusAdorner : Adorner { //被装饰的元素 FrameworkElement _adornedElement = null; //这里必须调用基类的构造函数 public FocusAdorner(UIElement adornedElement) : base(adornedElement) { } //通过复写onRender, 实现装饰器的呈现行为 protected override void OnRender(DrawingContext drawingContext) { } }
private void WindowLoaded(object sender, RoutedEventArgs e) { AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(_label); adornerLayer.Add(new ScaleAdorner(_label)); }
public class FocusAdorner : Adorner { public FocusAdorner(UIElement adornedElement) : base(adornedElement) { this.IsHitTestVisible = false; } protected override void OnRender(DrawingContext drawingContext) { base.OnRender(drawingContext); Rect adornedElementRect = new Rect(this.AdornedElement.RenderSize); Pen renderPen = new Pen(new SolidColorBrush(Colors.Red), 1.0); drawingContext.DrawRectangle(new SolidColorBrush(Colors.Transparent), renderPen,adornedElementRect); } }
private void WindowLoaded(object sender, RoutedEventArgs e) { AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(texBox1); adornerLayer.Add(new ScaleAdorner(texBox1)); }
当TextBox获得焦点时效果:
原文:http://www.cnblogs.com/infly123/p/3900196.html