private static void OnDataObjectChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var attached = DragDropManager.GetIsDragSourceAttached(d); if (!attached) { DragDropManager.SetIsDragSourceAttached(d, true); (( UIElement )d).PreviewMouseLeftButtonDown += (s, args) => { var _startPoint = args.GetPosition(null); DragDropManager.SetStartPoint(d, _startPoint); }; /* * We cannot use "MouseLeftButtonDown" because on the * TreeView control is never fired, maybe is handled by * someone else in the processing pipeline. */ //( ( UIElement )d ).MouseLeftButtonDown += ( s, args ) => //{ // var _startPoint = args.GetPosition( null ); // DragDropManager.SetStartPoint( d, _startPoint ); //}; (( UIElement )d).MouseMove += (s, args) => { var isDragging = DragDropManager.GetIsDragging(d); if (args.LeftButton == MouseButtonState.Pressed && !isDragging) { var position = args.GetPosition(null); var _startPoint = DragDropManager.GetStartPoint(d); if (Math.Abs(position.X - _startPoint.X) > SystemParameters.MinimumHorizontalDragDistance || Math.Abs(position.Y - _startPoint.Y) > SystemParameters.MinimumVerticalDragDistance) { StartDrag(d, args); } } }; } }
static void StartDrag(DependencyObject d, MouseEventArgs e) { var sourceItem = DragDropManager.FindDragContainer(( DependencyObject )e.OriginalSource); DragDropManager.SetIsDragging(d, true); var obj = DragDropManager.GetDataObject(sourceItem); var objType = DragDropManager.GetDataObjectType(sourceItem); DataObject data = null; if (String.IsNullOrEmpty(objType)) { data = new DataObject(obj.GetType(), obj); } else { data = new DataObject(objType, obj); } var de = DragDrop.DoDragDrop(d, data, DragDropEffects.Move); DragDropManager.SetIsDragging(d, false); }
private static void OnOnDropCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var attached = DragDropManager.GetIsDropTargetAttached(d); if (!attached) { DragDropManager.SetIsDropTargetAttached(d, true); var ui = ( UIElement )d; ui.AllowDrop = true; var ctrl = ui as Control; if (ctrl != null) { var bkg = ctrl.GetValue(ItemsControl.BackgroundProperty); if (bkg == null) { ctrl.SetValue(ItemsControl.BackgroundProperty, Brushes.Transparent); } } ui.DragEnter += (s, args) => { var os = ( DependencyObject )args.OriginalSource; var command = DragDropManager.FindDragEnterCommand(os); if (command != null) { var dropTarget = DragDropManager.FindDropTarget(os); var cmdArgs = new DragEnterArgs( args.Data, args.KeyStates, dropTarget, args.AllowedEffects); if (command.CanExecute(cmdArgs)) { command.Execute(cmdArgs); } } }; ui.DragLeave += (s, args) => { var os = ( DependencyObject )args.OriginalSource; var command = DragDropManager.FindDragLeaveCommand(os); if (command != null) { var dropTarget = DragDropManager.FindDropTarget(os); var cmdArgs = new DragLeaveArgs( args.Data, args.KeyStates, dropTarget, args.AllowedEffects); if (command.CanExecute(cmdArgs)) { command.Execute(cmdArgs); } } }; ui.DragOver += (s, args) => { var os = ( DependencyObject )args.OriginalSource; var command = DragDropManager.FindDropCommand(os); if (command != null) { var dropTarget = DragDropManager.FindDropTarget(os); Point position = new Point(0, 0); if (os is IInputElement) { position = args.GetPosition(( IInputElement )os); } var cmdArgs = new DragOverArgs( args.Data, args.KeyStates, dropTarget, args.AllowedEffects, position); var result = command.CanExecute(cmdArgs); if (!result) { args.Effects = cmdArgs.Effects; args.Handled = true; } } else { args.Effects = args.AllowedEffects; args.Handled = true; } }; ui.Drop += (s, args) => { var os = ( DependencyObject )args.OriginalSource; var command = DragDropManager.FindDropCommand(os); if (command != null) { var dropTarget = DragDropManager.FindDropTarget(os); Point position = new Point(0, 0); if (os is IInputElement) { position = args.GetPosition(( IInputElement )os); } var cmdArgs = new DropArgs(args.Data, args.KeyStates, dropTarget, position); command.Execute(cmdArgs); } }; } }