Exemplo n.º 1
0
        private void uiAudioList_MouseMove(object sender, MouseEventArgs e)
        {
            if (ShouldStartDrag(e))
            {
                this.isDragging = true;

                var item = this.draggedItem.DataContext as IAudioListItemViewModel;
                Debug.Assert(item != null);
                if (item.ShowLyrics)
                {
                    item.SwitchLyricsStateCommand.Execute(null);
                }
                var capturePoint = e.GetPosition(this.draggedItem);
                this.draggedItemAdorner = new DragAdorner(this.dragScope, this.draggedItem, true, capturePoint, 0.7);

                layer = AdornerLayer.GetAdornerLayer(this.dragScope);
                layer.Add(this.draggedItemAdorner);
                this.draggedItem.Visibility = System.Windows.Visibility.Collapsed;

                DragDrop.AddPreviewDragOverHandler(this.dragScope, DragScopeDragOver);
                DragDrop.AddGiveFeedbackHandler(this.dragScope, DragScopeGiveFeedback);
                try
                {
                    DragDrop.DoDragDrop(this.draggedItem, item, DragDropEffects.Move);
                    this.draggedItem.CaptureMouse();
                }
                finally
                {
                    DragDrop.RemovePreviewDragOverHandler(this.dragScope, DragScopeDragOver);
                    DragDrop.RemoveGiveFeedbackHandler(this.dragScope, DragScopeGiveFeedback);
                    this.draggedItem.Visibility = System.Windows.Visibility.Visible;

                    if (layer != null && this.draggedItemAdorner != null)
                    {
                        layer.Remove(this.draggedItemAdorner);
                    }
                    this.draggedItemAdorner = null;
                    this.draggedItem        = null;

                    this.isDragging = false;
                }
            }
        }
        /// <summary>
        /// 执行拖拽操作,在进行了有效的拖拽操作时执行
        /// </summary>
        /// <param name="dragItem"></param>
        /// <param name="adornerSource"></param>
        private void DoDragDrop(object dragItem, UIElement adornerSource)
        {
            if (adornerSource != null)                                                                     //如果选中按钮不为空
            {
                var rect  = VisualTreeHelper.GetDescendantBounds(adornerSource);                           //获取拖拽节点的外边框
                var size  = new Size((double)Math.Ceiling(rect.Width), (double)Math.Ceiling(rect.Height)); //获取比节点大的整数值边框
                var brush = new VisualBrush(adornerSource);                                                //初始化包含节点的VisualBrush
                _adorner            = new DragDropAdorner(_dragScope, size, brush);                        //新建拖拽装饰器
                _adorner.Opacity    = 0.7;
                _adorner.Visibility = Visibility.Hidden;
            }

            DragDrop.AddPreviewDragEnterHandler(_dragScope, _dragEnter);
            DragDrop.AddPreviewDragOverHandler(_dragScope, _dragOver);
            DragDrop.AddPreviewDragLeaveHandler(_dragScope, _dragLeave);

            DragDrop.DoDragDrop(_source, dragItem, DragDropEffects.All); //执行拖拽方法,该方法会在拖拽结束后结束并返回
            DragFinished();                                              //执行拖拽结束方法

            DragDrop.RemovePreviewDragEnterHandler(_dragScope, _dragEnter);
            DragDrop.RemovePreviewDragOverHandler(_dragScope, _dragOver);
            DragDrop.RemovePreviewDragLeaveHandler(_dragScope, _dragLeave);
        }
        private void DoDragDrop(object dragItem, UIElement adornerSource)
        {
            if (adornerSource != null)
            {
                var rect  = VisualTreeHelper.GetDescendantBounds(adornerSource);
                var size  = new Size((double)Math.Ceiling(rect.Width), (double)Math.Ceiling(rect.Height));
                var brush = new VisualBrush(adornerSource);
                _adorner            = new DragDropAdorner(_dragScope, size, brush);
                _adorner.Opacity    = 0.7;
                _adorner.Visibility = Visibility.Hidden;
            }

            DragDrop.AddPreviewDragEnterHandler(_dragScope, _dragEnter);
            DragDrop.AddPreviewDragOverHandler(_dragScope, _dragOver);
            DragDrop.AddPreviewDragLeaveHandler(_dragScope, _dragLeave);

            var resultEffects = DragDrop.DoDragDrop(_source, dragItem, DragDropEffects.All);

            DragFinished(resultEffects);

            DragDrop.RemovePreviewDragEnterHandler(_dragScope, _dragEnter);
            DragDrop.RemovePreviewDragOverHandler(_dragScope, _dragOver);
            DragDrop.RemovePreviewDragLeaveHandler(_dragScope, _dragLeave);
        }
Exemplo n.º 4
0
        private void StartDrag(MouseEventArgs args)
        {
            IDataObject data        = null;
            UIElement   dragelement = null;

            // ADD THE DATA
            if (this.Callback != null)
            {
                DragDataWrapper dw = new DragDataWrapper();

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

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

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

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

                dw.Source = this.DragSource;
                dw.Shim   = this.Callback;

                Debug.Assert(this.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;
            }

            DragEventHandler dragOver  = null;
            DragEventHandler dragLeave = null;
            QueryContinueDragEventHandler queryContinue = null;
            GiveFeedbackEventHandler      giveFeedback  = null;
            DragDropEffects effects = this.GetDragDropEffects();
            DragDropEffects resultEffects;

            // Inprocess Drag  ...
            if (this.DragScope != null)
            {
                bool previousAllowDrop = this.DragScope.AllowDrop;

                this.adorner = new DragAdorner(this.DragScope, (UIElement)dragelement, true, this.Opacity);
                this.layer   = AdornerLayer.GetAdornerLayer(this.DragScope as Visual);

                this.layer.Add(this.adorner);

                if (this.DragScope != this.DragSource)
                {
                    this.DragScope.AllowDrop = true;

                    DragDrop.AddPreviewDragOverHandler((DependencyObject)this.DragScope, dragOver = new DragEventHandler(DragScope_DragOver));
                    DragDrop.AddPreviewDragLeaveHandler(this.DragScope, dragLeave = new DragEventHandler(DragScope_DragLeave));
                    DragDrop.AddPreviewQueryContinueDragHandler(this.DragSource, queryContinue = new QueryContinueDragEventHandler(OnQueryContinueDrag));
                }

                try
                {
                    this.IsDragging     = true;
                    this.mouseLeftScope = false;
                    resultEffects       = DragDrop.DoDragDrop(this.DragSource, data, effects);

                    this.DragFinished(resultEffects);
                }
                catch
                {
                    Debug.Assert(false);
                }

                if (this.DragScope != this.DragSource)
                {
                    this.DragScope.AllowDrop = previousAllowDrop;

                    DragDrop.RemovePreviewDragOverHandler(this.DragScope, dragOver);
                    DragDrop.RemovePreviewDragLeaveHandler(this.DragScope, dragLeave);
                    DragDrop.RemovePreviewQueryContinueDragHandler(this.DragSource, queryContinue);
                }
            }
            else
            {
                DragDrop.AddPreviewQueryContinueDragHandler(this.DragSource, queryContinue = new QueryContinueDragEventHandler(OnQueryContinueDrag));
                DragDrop.AddGiveFeedbackHandler(this.DragSource, giveFeedback = new GiveFeedbackEventHandler(OnGiveFeedback));

                this.IsDragging = true;

                if ((this.Callback.SupportedActions & DragDropProviderActions.Visual) != 0)
                {
                    this.CreateDragDropWindow(dragelement);
                    this.dragdropWindow.Show();
                }

                try
                {
                    resultEffects = DragDrop.DoDragDrop(this.DragSource, data, effects);
                }
                finally
                {
                }

                if ((this.Callback.SupportedActions & DragDropProviderActions.Visual) != 0)
                {
                    this.DestroyDragDropWindow();
                }

                DragDrop.RemovePreviewQueryContinueDragHandler(this.DragSource, OnQueryContinueDrag);
                DragDrop.AddGiveFeedbackHandler(this.DragSource, OnGiveFeedback);

                this.IsDragging = false;
                this.DragFinished(resultEffects);
            }
        }
Exemplo n.º 5
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);
            }
        }