Beispiel #1
0
        void DropTarget_Drop(object sender, DragEventArgs e)
        {
            IDataObject data = e.Data;

            DragDataWrapper dw = null;
            bool            isDataOperation = false;

            if (data.GetDataPresent(typeof(DragDataWrapper).ToString()))
            {
                dw = data.GetData(typeof(DragDataWrapper).ToString()) as DragDataWrapper;
                if ((dw.Shim.SupportedActions & DragDropProviderActions.Data) != 0)
                {
                    isDataOperation = true;
                }
            }

            var target = _dropTarget;

            // When dropping inside a listview, find the listview item we targetted
            if (_dropTarget is ListView)
            {
                target = (e.OriginalSource as DependencyObject).FindListViewItem();
            }

            if (!isDataOperation)
            {
                if (data.GetDataPresent(typeof(UIElement).ToString()))
                {
                    UIElement uie = (UIElement)data.GetData(typeof(UIElement).ToString());

                    if (Drop != null)
                    {
                        Drop(this, new DropEventArgs(uie, dw.Source, target));
                    }
                }
                else if (data.GetDataPresent(DataFormats.Text))
                {
                    string datastring = (string)data.GetData(DataFormats.Text);

                    if (Drop != null)
                    {
                        Drop(this, new DropEventArgs(datastring, dw.Source, target));
                    }
                }
            }
            else
            {
                if (target != null)
                {
                    if (Drop != null)
                    {
                        Drop(this, new DropEventArgs(dw.Data, dw.Source, target));
                    }
                }
            }
        }
        void StartDrag(MouseEventArgs args)
        {
            IDataObject data        = null;
            UIElement   dragelement = null;


            // ADD THE DATA
            if (_callback != null)
            {
                DragDataWrapper dw = new DragDataWrapper();

                data = new DataObject(typeof(DragDataWrapper).ToString(), dw);

                if ((_callback.SupportedActions & DragDropProviderActions.MultiFormatData) != 0)
                {
                    _callback.AppendData(ref data, args);
                }

                if ((_callback.SupportedActions & DragDropProviderActions.Data) != 0)
                {
                    dw.Data = _callback.GetData();
                }

                if ((_callback.SupportedActions & DragDropProviderActions.Visual) != 0)
                {
                    dragelement = _callback.GetVisual(args);
                }
                else
                {
                    dragelement = args.OriginalSource as UIElement;
                }

                dw.Source = _dragSource;
                dw.Shim   = _callback;
            }
            else
            {
                dragelement = args.OriginalSource as UIElement;
                data        = new DataObject(typeof(UIElement).ToString(), dragelement);
            }


            // mwa changed from:
            // if (dragelement == null || data == null || dragelement == this._dragSource)
            if (dragelement == null || data == null)
            {
                return;
            }

            DragEventHandler dragOver  = null;
            DragEventHandler dragLeave = null;
            QueryContinueDragEventHandler queryContinue = null;
            GiveFeedbackEventHandler      giveFeedback  = null;


            DragDropEffects effects = GetDragDropEffects();
            DragDropEffects resultEffects;

            // Inprocess Drag  ...
            if (DragScope != null)
            {
                bool previousAllowDrop = DragScope.AllowDrop;
                _adorner = new DragAdorner(DragScope, (UIElement)dragelement, true, Opacity);
                _layer   = AdornerLayer.GetAdornerLayer(DragScope as Visual);
                _layer.Add(_adorner);

                if (DragScope != _dragSource)
                {
                    DragScope.AllowDrop = true;
                    System.Windows.DragDrop.AddPreviewDragOverHandler((DependencyObject)DragScope, dragOver = new DragEventHandler(DragScope_DragOver));
                    System.Windows.DragDrop.AddPreviewDragLeaveHandler(DragScope, dragLeave = new DragEventHandler(DragScope_DragLeave));
                    System.Windows.DragDrop.AddPreviewQueryContinueDragHandler(_dragSource, queryContinue = new QueryContinueDragEventHandler(onQueryContinueDrag));
                }

                try
                {
                    IsDragging           = true;
                    this._mouseLeftScope = false;
                    resultEffects        = System.Windows.DragDrop.DoDragDrop(_dragSource, data, effects);
                    DragFinished(resultEffects);
                }
                catch
                {
                }

                if (DragScope != _dragSource)
                {
                    DragScope.AllowDrop = previousAllowDrop;
                    System.Windows.DragDrop.RemovePreviewDragOverHandler(DragScope, dragOver);
                    System.Windows.DragDrop.RemovePreviewDragLeaveHandler(DragScope, dragLeave);
                    System.Windows.DragDrop.RemovePreviewQueryContinueDragHandler(_dragSource, queryContinue);
                }
            }
            else
            {
                System.Windows.DragDrop.AddPreviewQueryContinueDragHandler(_dragSource, queryContinue = new QueryContinueDragEventHandler(onQueryContinueDrag));
                System.Windows.DragDrop.AddGiveFeedbackHandler(_dragSource, giveFeedback = new GiveFeedbackEventHandler(onGiveFeedback));
                IsDragging = true;
                CreateDragDropWindow(dragelement);
                this._dragdropWindow.Show();
                try
                {
                    resultEffects = System.Windows.DragDrop.DoDragDrop(_dragSource, data, effects);
                }
                finally { }
                DestroyDragDropWindow();
                System.Windows.DragDrop.RemovePreviewQueryContinueDragHandler(_dragSource, onQueryContinueDrag);
                System.Windows.DragDrop.AddGiveFeedbackHandler(_dragSource, onGiveFeedback);
                IsDragging = false;
                DragFinished(resultEffects);
            }
        }
Beispiel #3
0
        void StartDrag(MouseEventArgs args)
        {
            IDataObject data = null;
            UIElement dragelement = null;

            // ADD THE DATA
            if (_callback != null)
            {
                DragDataWrapper dw = new DragDataWrapper();

                data = new DataObject(typeof(DragDataWrapper).ToString(), dw);

                if ((_callback.SupportedActions & DragDropProviderActions.MultiFormatData) != 0)
                    _callback.AppendData(ref data, args);

                if ((_callback.SupportedActions & DragDropProviderActions.Data) != 0)
                    dw.Data = _callback.GetData();

                if ((_callback.SupportedActions & DragDropProviderActions.Visual) != 0)
                    dragelement = _callback.GetVisual(args);
                else
                    dragelement = args.OriginalSource as UIElement;

                dw.Source = _dragSource;
                dw.Shim = _callback;
            }
            else
            {

                dragelement = args.OriginalSource as UIElement;
                data = new DataObject(typeof(UIElement).ToString(), dragelement);
            }

            // mwa changed from:
            // if (dragelement == null || data == null || dragelement == this._dragSource)
            if (dragelement == null || data == null)
                return;

            DragEventHandler dragOver = null;
            DragEventHandler dragLeave = null;
            QueryContinueDragEventHandler queryContinue = null;
            GiveFeedbackEventHandler giveFeedback = null;

            DragDropEffects effects = GetDragDropEffects();
            DragDropEffects resultEffects;

            // Inprocess Drag  ...
            if (DragScope != null)
            {
                bool previousAllowDrop = DragScope.AllowDrop;
                _adorner = new DragAdorner(DragScope, (UIElement)dragelement, true, Opacity);
                _layer = AdornerLayer.GetAdornerLayer(DragScope as Visual);
                _layer.Add(_adorner);

                if (DragScope != _dragSource)
                {
                    DragScope.AllowDrop = true;
                    System.Windows.DragDrop.AddPreviewDragOverHandler((DependencyObject)DragScope, dragOver = new DragEventHandler(DragScope_DragOver));
                    System.Windows.DragDrop.AddPreviewDragLeaveHandler(DragScope, dragLeave = new DragEventHandler(DragScope_DragLeave));
                    System.Windows.DragDrop.AddPreviewQueryContinueDragHandler(_dragSource, queryContinue = new QueryContinueDragEventHandler(onQueryContinueDrag));
                }

                try
                {
                    IsDragging = true;
                    this._mouseLeftScope = false;
                    resultEffects = System.Windows.DragDrop.DoDragDrop(_dragSource, data, effects);
                    DragFinished(resultEffects);
                }
                catch
                {
                }

                if (DragScope != _dragSource)
                {
                    DragScope.AllowDrop = previousAllowDrop;
                    System.Windows.DragDrop.RemovePreviewDragOverHandler(DragScope, dragOver);
                    System.Windows.DragDrop.RemovePreviewDragLeaveHandler(DragScope, dragLeave);
                    System.Windows.DragDrop.RemovePreviewQueryContinueDragHandler(_dragSource, queryContinue);
                }
            }
            else
            {

                System.Windows.DragDrop.AddPreviewQueryContinueDragHandler(_dragSource, queryContinue = new QueryContinueDragEventHandler(onQueryContinueDrag));
                System.Windows.DragDrop.AddGiveFeedbackHandler(_dragSource, giveFeedback = new GiveFeedbackEventHandler(onGiveFeedback));
                IsDragging = true;
                CreateDragDropWindow(dragelement);
                this._dragdropWindow.Show();
                try
                {
                    resultEffects = System.Windows.DragDrop.DoDragDrop(_dragSource, data, effects);
                }
                finally { }
                DestroyDragDropWindow();
                System.Windows.DragDrop.RemovePreviewQueryContinueDragHandler(_dragSource, onQueryContinueDrag);
                System.Windows.DragDrop.AddGiveFeedbackHandler(_dragSource, onGiveFeedback);
                IsDragging = false;
                DragFinished(resultEffects);
            }
        }