WPF拖放源代码

时间:2013-01-08 21:19:18

标签: c# wpf drag-and-drop adorner

几乎所有关于在BPF中处理拖放的stackoverflow的问题都会引用this article。但是,文章的source code丢失了。有没有人有副本或知道在哪里找到副本?

P.S。我主要关注的是文章中的这一行:“DragAdorner中有更多代码,但主要是用于定位装饰,因为拖动正在发生......请参考示例......”我不知道他是如何定位阻力装饰的。

3 个答案:

答案 0 :(得分:3)

我有一些基于原始系列博客文章的工作代码。完整的代码太长了,无法在此发布;我posted it to PasteBin

DragAdorner中的代码非常简单:

internal sealed class DragAdorner : Adorner
{
   private readonly UIElement _child;
   private readonly double _xCenter;
   private readonly double _yCenter;
   private double _leftOffset;
   private double _topOffset;

   public DragAdorner(UIElement owner, UIElement child, bool useVisualBrush, double opacity) : base(owner)
   {
      if (!useVisualBrush)
      {
         _child = child;
      }
      else
      {
         var size = GetRealSize(child);
         _xCenter = size.Width / 2;
         _yCenter = size.Height / 2;

         _child = new Rectangle
         {
            RadiusX = 3,
            RadiusY = 3,
            Width = size.Width,
            Height = size.Height,
            Fill = new VisualBrush(child)
            {
               Opacity = opacity,
               AlignmentX = AlignmentX.Left,
               AlignmentY = AlignmentY.Top,
               Stretch = Stretch.None,
            },
         };
      }
   }

   protected override int VisualChildrenCount
   {
      get { return 1; }
   }

   public double LeftOffset
   {
      get
      {
         return _leftOffset + _xCenter;
      }
      set
      {
         _leftOffset = value - _xCenter;
         UpdatePosition();
      }
   }

   public double TopOffset
   {
      get
      {
         return _topOffset + _yCenter;
      }
      set
      {
         _topOffset = value - _yCenter;
         UpdatePosition();
      }
   }

   private static Size GetRealSize(UIElement child)
   {
      return child == null ? Size.Empty : child.RenderSize;
   }

   public void UpdatePosition(Point point)
   {
      _leftOffset = point.X;
      _topOffset = point.Y;
      UpdatePosition();
   }

   public void UpdatePosition()
   {
      var adorner = Parent as AdornerLayer;
      if (adorner != null) adorner.Update(AdornedElement);
   }

   protected override Visual GetVisualChild(int index)
   {
      if (0 != index) throw new ArgumentOutOfRangeException("index");
      return _child;
   }

   protected override Size MeasureOverride(Size availableSize)
   {
      _child.Measure(availableSize);
      return _child.DesiredSize;
   }

   protected override Size ArrangeOverride(Size finalSize)
   {
      _child.Arrange(new Rect(_child.DesiredSize));
      return finalSize;
   }

   public override GeneralTransform GetDesiredTransform(GeneralTransform transform)
   {
      var result = new GeneralTransformGroup();
      result.Children.Add(new TranslateTransform(_leftOffset, _topOffset));

      var baseTransform = base.GetDesiredTransform(transform);
      if (baseTransform != null) result.Children.Add(baseTransform);

      return result;
   }
}

答案 1 :(得分:1)

通过电子邮件发送作者并得到回复,表示他不再拥有该文章的源代码。

答案 2 :(得分:0)

查看bea stollnitz的博客

http://www.zagstudio.com/blog/488#.UQRZCL9fA7X 

这个拖放样本是WPF相对较新时的第一个样本。这无疑帮助了我和我的同事......

相关问题