コード例 #1
0
        private bool IsValidDropTarget(UIElement target)
        {
            FrameworkElement dropTarget;

            if (target == null)
            {
                SwitchDropTarget(null);
                Console.WriteLine("IsValidDropTarget ====> target is NULL");
                return(false);
            }

            TreeViewItem container = GetNearestContainer(target);

            if (container == null)
            {
                SwitchDropTarget(null);
                Console.WriteLine("IsValidDropTarget ====> container is NULL");
                return(false);
            }
            Node_Base dataTarget = container.Header as Node_Base;

            if (dataTarget == null)
            {
                SwitchDropTarget(null);
                Console.WriteLine("IsValidDropTarget ====> dataTarget is null");
                return(false);
            }

            /*
             *  if (CurrentDropTarget != null)
             *      CurrentDropTarget.DropArea = Node_Base.enItemDropArea.None;
             *
             *  CurrentDropTarget = source;
             *  CurrentDropTarget.DropArea = targetDropArea_;
             */
            #region check PART_TopDropArea
            dropTarget = FindAncestorByName(target, "PART_TopDropArea");
            if (dropTarget != null)
            {
                dataTarget.DropArea = Node_Base.enItemDropArea.Top;
                Console.WriteLine("IsValidDropTarget ====> Hovering on PART_TopDropArea");
                SwitchDropTarget(dataTarget);
                return(true);
            }
            #endregion

            #region check PART_BottomDropArea
            dropTarget = FindAncestorByName(target, "PART_BottomDropArea");
            if (dropTarget != null)
            {
                dataTarget.DropArea = Node_Base.enItemDropArea.Bottom;
                Console.WriteLine("IsValidDropTarget ====> Hovering on PART_BottomDropArea");
                SwitchDropTarget(dataTarget);
                return(true);
            }
            #endregion

            //TreeViewItem container = GetNearestContainer(target);

            if (container == null)
            {
                Console.WriteLine("IsValidDropTarget ====> container is NULL");
                SwitchDropTarget(null);
                return(false);
            }

            if (sourceNode_.Children.Contains(targetNode_))
            {
                Console.WriteLine("IsValidDropTarget ====> cannot drop parent on child");
                SwitchDropTarget(null);
                return(false);
            }

            //Node_Base dataTarget = container.Header as Node_Base;

            if (sourceNode_ == dataTarget)
            {
                Console.WriteLine("IsValidDropTarget ====> itself");
                SwitchDropTarget(null);
                return(false);
            }
            if (dataTarget is Node_Cell)
            {
                Console.WriteLine("IsValidDropTarget ====> cannot drop over cell");
                SwitchDropTarget(null);
                return(false);
            }

            if (dataTarget is Node_Group)
            {
                dataTarget.DropArea = Node_Base.enItemDropArea.Center;
                SwitchDropTarget(dataTarget);
                dataTarget.IsExpanded = true;
            }
            else
            {
                dataTarget.DropArea = Node_Base.enItemDropArea.None;
                SwitchDropTarget(null);
            }

            Console.WriteLine("IsValidDropTarget ====> Hovering on " + dataTarget.Name);

            return(dataTarget is Node_Group);
        }
コード例 #2
0
        private void TreeView_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton != MouseButtonState.Pressed)
            {
                return;
            }

            Point currentPosition = e.GetPosition(TheTreeView);

            // Note: This should be based on some accessibility number and not just 2 pixels
            if ((Math.Abs(currentPosition.X - _lastMouseDown.X) > 5.0) ||
                (Math.Abs(currentPosition.Y - _lastMouseDown.Y) > 5.0))
            {
                Node_Base selectedItem = (Node_Base)TheTreeView.SelectedItem;

                sourceNode_ = selectedItem;
                Console.WriteLine("TreeView_MouseMove >>> selectedItem = " + selectedItem.Name);
                if (selectedItem == null)
                {
                    return;
                }

                //TreeViewItem container = (TreeViewItem)(TheTreeView.ItemContainerGenerator.ContainerFromIndex(TheTreeView.Items.CurrentPosition));
                TreeViewItem container = (TreeViewItem)(TheTreeView.ItemContainerGenerator.ContainerFromItem(sourceNode_));
                container = GetTreeViewItem(e.GetPosition(TheTreeView));

                if (container == null)
                {
                    return;
                }

                IsDragInProgress = true;
                DragDropEffects finalDropEffect = DragDrop.DoDragDrop(container, selectedItem, DragDropEffects.Move);
                IsDragInProgress = false;

                // check if we are trying to drop a node onto one of its children
                if (selectedItem.ContainsNode(targetNode_))
                {
                    return;
                }

                if (selectedItem == targetNode_)
                {
                    return;
                }

                // verificare che il target non sia figlio del selectedItem
                if ((finalDropEffect == DragDropEffects.Move) && (targetNode_ != null))
                {
                    #region  A Move drop was accepted

                    selectedItem.Parent.Children.Remove(selectedItem);

                    switch (targetDropArea_)
                    {
                    case Node_Base.enItemDropArea.Top:
                        targetNode_.Parent.Children.Insert(targetNode_.Index, selectedItem);
                        break;

                    case Node_Base.enItemDropArea.Center:
                        targetNode_.Children.Add(selectedItem);
                        break;

                    case Node_Base.enItemDropArea.Bottom:
                        targetNode_.Parent.Children.Insert(targetNode_.Index + 1, selectedItem);
                        break;

                    default:
                        break;
                    }
                    SelectNode(selectedItem);
                    #endregion
                }
                root_.UpdateIndexes();

                sourceNode_      = null;
                targetNode_      = null;
                IsDragInProgress = false;
            }
        }