示例#1
0
        protected override void OnDragOver(DragEventArgs e)
        {
            base.OnDragEnter(e);

            HitTestResult result = VisualTreeHelper.HitTest(this, e.GetPosition(this));

            if ((result.VisualHit as UIElement).IsDescendantOf(this))
            {
                UIElement            element    = (result.VisualHit as UIElement);
                DragDropTreeViewItem targetNode = GetNearestContainer(e.Source as UIElement);
                if (e.GetPosition(targetNode).Y < targetNode.ActualHeight * 0.2f)
                {
                    // meter em cima
                    //Console.WriteLine("cima");
                    DragDropHelper.insertionPlace = DragDropHelper.InsertionPlace.Top;
                    DragDropHelper.CreateInsertionAdorner(targetNode, true);
                }
                else if (e.GetPosition(targetNode).Y > targetNode.ActualHeight * 0.8f)
                {
                    //Console.WriteLine("baixo");
                    DragDropHelper.insertionPlace = DragDropHelper.InsertionPlace.Bottom;
                    DragDropHelper.CreateInsertionAdorner(targetNode, false);
                }
                else
                {
                    //Console.WriteLine("centro");
                    DragDropHelper.RemoveInsertionAdorner();
                    DragDropHelper.insertionPlace = DragDropHelper.InsertionPlace.Center;
                    var converter = new System.Windows.Media.BrushConverter();
                    Background = (Brush)converter.ConvertFromString("#555");
                }
            }
        }
示例#2
0
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            HitTestResult result = VisualTreeHelper.HitTest(this, e.GetPosition(this));

            // Move the dragged node when the left mouse button is used. EditorUtils.GetParent(result.VisualHit, 2) ==
            if (e.LeftButton == MouseButtonState.Pressed && CanDrag && (result.VisualHit as UIElement) != null && (result.VisualHit as UIElement).IsDescendantOf(this))
            {
                try
                {
                    DragDrop.DoDragDrop(this, this, DragDropEffects.Move);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            DragDropTreeViewItem targetNode = GetNearestContainer(e.Source as UIElement);

            if (targetNode == null)
            {
                DragDropHelper.RemoveInsertionAdorner();
            }
            //Console.WriteLine(this.PointFromScreen(Mouse.GetPosition(targetNode)));
        }
示例#3
0
 protected override void OnDragLeave(DragEventArgs e)
 {
     base.OnDragLeave(e);
     Background = Brushes.Transparent;
     DragDropHelper.RemoveInsertionAdorner();
 }
示例#4
0
        protected override void OnDrop(DragEventArgs e)
        {
            base.OnDrop(e);

            // Retrieve the client coordinates of the drop location.
            Point targetPoint = e.GetPosition(this);

            try
            {
                // Retrieve the node at the drop location.
                DragDropTreeViewItem targetNode = GetNearestContainer(e.Source as UIElement);

                DragDropHelper.RemoveInsertionAdorner();

                // Retrieve the node that was dragged.
                DragDropTreeViewItem draggedNode = (DragDropTreeViewItem)e.Data.GetData(typeof(DragDropTreeViewItem));

                if (draggedNode == null)
                {
                    draggedNode = (DragDropTreeViewItem)e.Data.GetData(typeof(ExplorerTreeViewItem));
                }

                if (targetNode == null || draggedNode == null)
                {
                    return;
                }

                // Confirm that the node at the drop location is not
                // the dragged node or a descendant of the dragged node.
                if (!draggedNode.Equals(targetNode) && !ContainsNode(draggedNode, targetNode))
                {
                    CancelEventArgs evt = new CancelEventArgs();
                    OnDragDropSuccess(draggedNode, targetNode, evt);

                    if (!evt.Cancel)
                    {
                        // If it is a move operation, remove the node from its current
                        // location and add it to the node at the drop location.
                        if (e.Effects == DragDropEffects.Move)
                        {
                            List <TreeViewItem> items = TreeViewExtension.GetSelectedTreeViewItems(TreeViewExtension.GetTree(targetNode));

                            if (items != null && items.Count > 0)
                            {
                                // multi selection drag:
                                foreach (var ti in items)
                                {
                                    (ti.Parent as ItemsControl).Items.Remove(ti);

                                    if (DragDropHelper.insertionPlace == DragDropHelper.InsertionPlace.Center)
                                    {
                                        targetNode.Items.Add(ti);
                                    }
                                    else
                                    {
                                        int index = (targetNode.Parent as ItemsControl).ItemContainerGenerator.IndexFromContainer(targetNode);
                                        if (index < 0)
                                        {
                                            index = 0;
                                        }

                                        if (DragDropHelper.insertionPlace == DragDropHelper.InsertionPlace.Bottom)
                                        {
                                            index++;
                                        }

                                        (targetNode.Parent as ItemsControl).Items.Insert(index, ti);
                                    }

                                    ti.IsSelected = true;

                                    ReApplyStyle(ti as DragDropTreeViewItem, "IgniteMultiTreeViewItem");
                                }
                            }
                            else
                            {
                                (draggedNode.Parent as ItemsControl).Items.Remove(draggedNode);

                                if (DragDropHelper.insertionPlace == DragDropHelper.InsertionPlace.Center)
                                {
                                    targetNode.Items.Add(draggedNode);
                                }
                                else
                                {
                                    int index = (targetNode.Parent as ItemsControl).ItemContainerGenerator.IndexFromContainer(targetNode);
                                    if (index < 0)
                                    {
                                        index = 0;
                                    }

                                    if (DragDropHelper.insertionPlace == DragDropHelper.InsertionPlace.Bottom)
                                    {
                                        index++;
                                    }

                                    (targetNode.Parent as ItemsControl).Items.Insert(index, draggedNode);
                                }

                                draggedNode.IsSelected = true;

                                ReApplyStyle(draggedNode, "IgniteTreeViewItem");
                            }
                        }
                        // OPTIONAL:
                        // If it is a copy operation, clone the dragged node
                        // and add it to the node at the drop location.
                        //else if (e.Effects == DragDropEffects.Copy)
                        //{
                        //    targetNode.Items.Add((DragDropTreeViewItem)draggedNode);
                        //}

                        // Expand the node at the location
                        // to show the dropped node.
                        targetNode.IsExpanded = true;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }