8

Almost all the questions on stackoverflow dealing with drag and drop in WPF refer back to this article. However, the source code for the article is missing. Does anyone have a copy or know where to find a copy?

P.S. My main concern is this line from the article: "There is more code in DragAdorner, but mostly used for positioning the adorner as the drag is happening... please refer to the sample..." I have no idea how he positions the drag adorner.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Nathan Hillyer
  • 1,959
  • 1
  • 18
  • 22

3 Answers3

3

I have some working code based on the original series of blog posts. The full code is too long to post here; I've posted it to PasteBin.

The code in the DragAdorner is quite simple:

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;
   }
}
Richard Deeming
  • 29,830
  • 10
  • 79
  • 151
1

E-mailed the author and got a response that he no longer has the source code for the article.

Nathan Hillyer
  • 1,959
  • 1
  • 18
  • 22
0

check out bea stollnitz's blog

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

this drag and drop sample was one of the first ones back when WPF relatively new. It definitely helped me and my co-workers ...

denis morozov
  • 6,236
  • 3
  • 30
  • 45
  • Also down now. Article can be found on the Web Archive https://web.archive.org/web/20150912232819/http://www.zagstudio.com:80/blog/488#.WpnN2qjwa70 (Hopefully those web archive links are valid for a while) – FrankyB Mar 02 '18 at 22:22