示例#1
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;

                System.Diagnostics.Debug.Assert(DragScope == null, "The DragDataWrapper is meant for in-proc...  Sorry for asserting, just wanted to confirm.. comment out assertion if needed");
            }
            else
            {

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

            if (dragelement == null || data == null || dragelement == this._dragSource)
                return;

            #if DEBUG
            // Add it as Text ... good for testing purposes

            if ( !data.GetDataPresent(DataFormats.Text))
                data.SetData(DataFormats.Text, "abcd" );
            #endif

            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;
                    DragDrop.AddPreviewDragOverHandler((DependencyObject)DragScope, dragOver = new DragEventHandler(DragScope_DragOver));
                    DragDrop.AddPreviewDragLeaveHandler(DragScope, dragLeave = new DragEventHandler(DragScope_DragLeave));
                    DragDrop.AddPreviewQueryContinueDragHandler(_dragSource, queryContinue = new QueryContinueDragEventHandler(onQueryContinueDrag));
                }

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

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

                DragDrop.AddPreviewQueryContinueDragHandler(_dragSource, queryContinue = new QueryContinueDragEventHandler(onQueryContinueDrag));
                DragDrop.AddGiveFeedbackHandler(_dragSource, giveFeedback = new GiveFeedbackEventHandler( onGiveFeedback));
                IsDragging = true;
                CreateDragDropWindow(dragelement);
                this._dragdropWindow.Show();
                try
                {
                    resultEffects = DragDrop.DoDragDrop(_dragSource, data, effects);
                }
                finally { }
                DestroyDragDropWindow();
                DragDrop.RemovePreviewQueryContinueDragHandler(_dragSource, onQueryContinueDrag);
                DragDrop.AddGiveFeedbackHandler(_dragSource, onGiveFeedback);
                IsDragging = false;
                DragFinished(resultEffects);
            }
        }
示例#2
0
        protected void DragFinished(DragDropEffects ret)
        {
            System.Windows.Input.Mouse.Capture(null);
            if (IsDragging)
            {
                if (DragScope != null)
                {
                    AdornerLayer.GetAdornerLayer(DragScope).Remove(_adorner);
                    _adorner = null;
                }
                else
                {
                    DestroyDragDropWindow();
                }

            }
            IsDragging = false;
        }