装饰器学习心得

装饰器学习心得

0 界定

装饰器能够用于给表明的控制加上一些装饰设计的实际效果;

1 实际效果

1 选中

电脑鼠标按住获得当今点

   startPoint = parameter.GetPosition(Application.Current.MainWindow);

伴随着电脑鼠标挪动获得未尾点,持续再次画矩形框

 Point tempEndPoint = parameter.GetPosition(Application.Current.MainWindow);

1.1 画临时性矩形框

 private void DrawRect(Point endPoint, Point startPoint)
        {
            if (selectRect == null)
            {
                selectRect = new Rectangle();
                selectRect.Fill = new SolIDColorBrush(Colors.Gray) { Opacity = 0.1 };
                selectRect.Margin = new Thickness(startPoint.X, startPoint.Y, 0, 0);
                Controls.Add(selectRect);
            }
            selectRect.Width = Math.Abs(endPoint.X - startPoint.X);
            selectRect.Height = Math.Abs(endPoint.Y - startPoint.Y);
        }

2 加上装饰器

装饰器是 FrameworkElement 关联到的自定 UIElement , 装饰器展现在装饰器层中。

  • 装饰器层是自始至终坐落于装饰设计原素或装饰设计原素结合以上。
  • 装饰器一般应用坐落于装饰设计原素左上端的规范 3D 座标起点,相对性于其关联到的原素开展精准定位。
  • 装饰器自始至终以由此可见的方法坐落于控制顶端,没法应用 z 次序调用。

2.1 基本加上装饰器

基本作法一般在装饰器层加上装饰器后,在装饰器时会以调用Render的方法加上实际效果

// Adorners must subclass the abstract base class Adorner.
public class SimpleCircleAdorner : Adorner
{
  // Be sure to call the base class constructor.
  public SimpleCircleAdorner(UIElement adornedElement)
    : base(adornedElement)
  {
  }

  // 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);
  }
}

2.1 装饰器怎样加上自定控制?

装饰器沒有得出一个立即的方式 去加上自定控制,且装饰器与外部的联络只有一个被装饰设计的控制,那麼大家怎样在数据可视化树枝加上自定控制呢?

我们可以根据间接性的方式 :

The AddVisualChild method is a notification to the base class that you've acquired a new child into your collection. It doesn't actually add the child to any collection, but it lets the Visual system know that it needs to redraw. This sounds a bit funny, but it's consistent with the way the logical tree is managed as well.

Similarly, when you remove a Visual from your child collection you must call RemoveVisualChild, so the Visual system knows to stop tracking and displaying that Visual. If you'd like, and it sounds appropriate in your scenario, you don't have to use an actual collection for your child visuals. You can Simply have a member variable of type Visual, which you can think of as a collection of maximum length 1. Your implementation of GetVisualChild should return the value of that variable, and your implementation of VisualChildrenCount should probably return 0 if that variable is null, or 1 if it's non-null.

根据调用GetVisualChild方式 使数据可视化树重绘

2.2 控制和装饰器怎样完成连动实际效果

假如想完成预估的实际效果,必须装饰器B和被装饰设计控制A连动,例如当A产生变形时,B也产生变形

一切正常的,大家可以用A去通告B,可是装饰器做为单独的一层,不应该和A造成联络

我们可以根据调用ArrangeOverride方式 ,当再次排列的情况下给装饰器内的自定控制一个自身的尺寸(部位)

  public Rect AdonerArrange(Size finalSize)
        {
            int margin = 10;
            return new Rect(-margin, -margin, finalSize.Width   2 * margin, finalSize.Height   2 * margin);
        }

3 移动事例

在自定控制层,大家根据装饰器取得被装饰器的控制

自定装饰器控制-->装饰器层-->被装饰设计控制

被装饰设计控制:grid

装饰器层:layer

自定装饰器控制:contentControl

 			 var layer = AdornerLayer.GetAdornerLayer(grid);
            DragDropControl contentControl = new DragDropControl(grid);
            contentControl.Background = new SolidColorBrush(Colors.Black) { Opacity = 0.1 };
            layer.Add(new ContentAdorner(grid, contentControl));

以后我们在自定装饰器控制contentControl中实际操作被装饰设计控制grid:

        bool isCanMove = false;

        private void thumMove_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
        {
            isCanMove = true;
        }

        private void thumMove_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            if (isCanMove)
            {
                InkCanvas.SetLeft(_adornedElement, InkCanvas.GetLeft(_adornedElement)   e.HorizontalChange);
                InkCanvas.SetTop(_adornedElement, InkCanvas.GetTop(_adornedElement)   e.VerticalChange);
            }
        }

        private void thumMove_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
        {
            isCanMove = false;
        }

这时大家根据contentControl控制移动被装饰设计的控制grid

而contentControl根据AdonerArrange方式 追随grid开展移动

 public Rect AdonerArrange(Size finalSize)
        {
            int margin = 10;
            return new Rect(-margin, -margin, finalSize.Width   2 * margin, finalSize.Height   2 * margin);

        }

完成了:

当控制沒有装饰器时,并不具有移动工作能力。

当控制有改装饰器装饰时,具有了移动工作能力。

即基本装饰器能够加上实际效果,自定控制装饰器能够加上一些工作能力。

Demo:https://GitHub.com/tiancai4652/MyDecorator

评论(0条)

刀客源码 游客评论