示例#1
0
        /// <summary>
        /// First determine whether the drag data is supported.
        /// Second determine what operation to do (copy, move, link).
        /// And finally handle the actual drop when <code>bDrop</code> is true.
        /// </summary>
        /// <param name="bDrop">True to perform an actual drop, otherwise just return e.Effects</param>
        /// <param name="sender">DragDrop event <code>sender</code></param>
        /// <param name="e">DragDrop event arguments</param>
        private void DragOverOrDrop(bool bDrop, object sender, DragEventArgs e)
        {
            CanvasDataProvider <TContainer, TObject> dataProvider = this.GetData(e) as CanvasDataProvider <TContainer, TObject>;

            if (dataProvider != null)
            {
                TObject dragSourceObject = dataProvider.SourceObject as TObject;
                Debug.Assert(dragSourceObject != null);

                TContainer dropContainer = sender as TContainer;

                if (dropContainer != null)
                {
                    if (bDrop)
                    {
                        dataProvider.Unparent();
                        dropContainer.Children.Add(dragSourceObject);

                        Point dropPosition = e.GetPosition(dropContainer);
                        Point objectOrigin = dataProvider.StartPosition;
                        Canvas.SetLeft(dragSourceObject, dropPosition.X - objectOrigin.X);
                        Canvas.SetTop(dragSourceObject, dropPosition.Y - objectOrigin.Y);
                    }
                    e.Effects = DragDropEffects.Move;
                    e.Handled = true;
                }
                else
                {
                    e.Effects = DragDropEffects.None;
                    e.Handled = true;
                }
            }
        }
示例#2
0
        /// <summary>
        /// First determine whether the drag data is supported.
        /// Second determine what operation to do (move, link).
        /// And finally handle the actual drop when <code>bDrop</code> is true.
        ///
        /// Note that a new button needs to be created for the toolbar.
        /// </summary>
        /// <param name="bDrop">True to perform an actual drop, otherwise just return e.Effects</param>
        /// <param name="sender">DragDrop event <code>sender</code></param>
        /// <param name="e">DragDrop event arguments</param>
        private void DragOverOrDrop(bool bDrop, object sender, DragEventArgs e)
        {
            CanvasDataProvider <TContainer, TObject> dataProvider = this.GetData(e) as CanvasDataProvider <TContainer, TObject>;

            if (dataProvider != null)
            {
                TContainer dragSourceContainer = dataProvider.SourceContainer as TContainer;
                TObject    dragSourceObject    = dataProvider.SourceObject as TObject;
                Debug.Assert(dragSourceObject != null);
                Debug.Assert(dragSourceContainer != null);

                ItemsControl dropContainer = sender as ItemsControl;
                TObject      dropTarget    = e.Source as TObject;
                if (dropTarget == null)
                {
                    dropTarget = DragDropFramework.Utilities.FindParentControlExcludingMe <TObject>(e.Source as DependencyObject);
                }

                if (dropContainer != null)
                {
                    if (bDrop)
                    {
                        dataProvider.Unparent();
                        Button button;
#if REUSE_SAME_BUTTON //|| true
                        button = dragSourceObject as Button;
#else
                        Button oldButton = dragSourceObject as Button;
                        button         = new Button();
                        button.Content = DragDropFramework.Utilities.CloneElement(oldButton.Content);
                        button.ToolTip = oldButton.ToolTip;
#endif
                        if (dropTarget == null)
                        {
                            dropContainer.Items.Add(button);
                        }
                        else
                        {
                            dropContainer.Items.Insert(dropContainer.Items.IndexOf(dropTarget), button);
                        }
                    }
                    e.Effects = (dropTarget == null) ? DragDropEffects.Move : DragDropEffects.Link;
                    e.Handled = true;
                }
                else
                {
                    e.Effects = DragDropEffects.None;
                    e.Handled = true;
                }
            }
        }