private Boolean CheckIfValidDrop(TreeViewItemDropEventArgs tviea)
        {
            SwagOption targetOpt  = (SwagOption)tviea.TargetItem.DataContext;
            SwagOption droppedOpt = (SwagOption)tviea.DroppedItem.DataContext;
            Boolean    valid      = true;
            MoveType   moveType   = GetMoveType(tviea);

            SwagOption tempOpt = targetOpt.Parent;

            while (valid && tempOpt != null)
            {
                if (droppedOpt == tempOpt) //Don't drop within own descendants
                {
                    valid = false;
                    break;
                }
                tempOpt = tempOpt.Parent;
            }

            if (!valid)
            {
                tviea.DragEventArgs.Effects = DragDropEffects.None;
                tviea.DragEventArgs.Handled = true;
            }

            return(valid);
        }
Beispiel #2
0
        private Boolean CheckIfValidDrop(TreeViewItemDropEventArgs tviea)
        {
            BooleanExpression targetExp  = (BooleanExpression)tviea.TargetItem.DataContext;
            BooleanExpression droppedExp = (BooleanExpression)tviea.DroppedItem.DataContext;
            Boolean           valid      = true;
            MoveType          moveType   = GetMoveType(tviea);

            if ((targetExp == RootExpression && (moveType == MoveType.Above || moveType == MoveType.Below)) || //Don't drop as root sibling
                targetExp == droppedExp)                                                                       //Don't drop to self
            {
                valid = false;
            }

            BooleanExpression tempExp = targetExp.Parent;

            while (valid && tempExp != null)
            {
                if (droppedExp == tempExp) //Don't drop within own descendants
                {
                    valid = false;
                    break;
                }
                tempExp = tempExp.Parent;
            }

            if (!valid)
            {
                tviea.DragEventArgs.Effects = DragDropEffects.None;
                tviea.DragEventArgs.Handled = true;
            }

            return(valid);
        }
Beispiel #3
0
        private void SwagItemsControl_TreeViewItemDropPreview(object sender, RoutedEventArgs e)
        {
            TreeViewItemDropEventArgs tviea = (TreeViewItemDropEventArgs)e;

            if (!CheckIfValidDrop(tviea))
            {
                return;
            }

            Boolean allowChildMove = false, allowSiblingMove = false;

            switch (tviea.TargetItem.DataContext)
            {
            case BooleanOperationExpression opExp:
                allowChildMove   = true;
                allowSiblingMove = true;
                break;

            case BooleanBooleanVariableExpression bbvExp:
                allowSiblingMove = true;
                break;

            case BooleanStringVariableExpression bsvExp:
                allowSiblingMove = true;
                break;
            }

            tviea.TargetItem.BorderBrush = (Brush)this.FindResource("MahApps.Brushes.Text");
            MoveType moveType = GetMoveType(tviea);

            switch (moveType)
            {
            case MoveType.Above:
                if (allowSiblingMove)
                {
                    tviea.TargetItem.BorderThickness = new Thickness(0, 1, 0, 0);
                }
                break;

            case MoveType.Below:
                if (allowSiblingMove)
                {
                    tviea.TargetItem.BorderThickness = new Thickness(0, 0, 0, 1);
                }
                break;

            case MoveType.Into:
                if (allowChildMove)
                {
                    tviea.TargetItem.BorderThickness = new Thickness(1, 0, 0, 0);
                }
                else
                {
                    tviea.DragEventArgs.Effects = DragDropEffects.None;
                    tviea.DragEventArgs.Handled = true;
                }
                break;
            }
        }
 private void ControlTreeView_PreviewDragLeave(object sender, DragEventArgs e)
 {
     if (AllowMove)
     {
         TreeViewItem targetItem = ((DependencyObject)e.OriginalSource).TryFindParent <TreeViewItem>();
         if (targetItem != null)
         {
             RoutedEventArgs ea = new TreeViewItemDropEventArgs(SwagItemsControl.TreeViewItemLeavePreviewEvent, e, targetItem, null);
             RaiseEvent(ea);
         }
     }
 }
        private void ControlTreeView_Drop(object sender, DragEventArgs e)
        {
            if (AllowMove)
            {
                TreeViewItem targetItem  = ((DependencyObject)e.OriginalSource).TryFindParent <TreeViewItem>();
                TreeViewItem droppedItem = (TreeViewItem)e.Data.GetData("TreeViewItemDrop");

                if (targetItem != null && droppedItem != null)
                {
                    RoutedEventArgs ea = new TreeViewItemDropEventArgs(SwagItemsControl.TreeViewItemDropEvent, e, targetItem, droppedItem);
                    RaiseEvent(ea);
                }
            }
        }
        private MoveType GetMoveType(TreeViewItemDropEventArgs tviea)
        {
            MoveType moveType = MoveType.None;

            FrameworkElement header  = tviea.TargetItem.FindVisualChild <FrameworkElement>("PART_Header");
            Point            p       = tviea.DragEventArgs.GetPosition(header);
            Double           quarter = header.ActualHeight / 4;

            if (p.Y + 2 < quarter * 1)
            {
                moveType = MoveType.Above;
            }
            else if (p.Y - 2 > quarter * 3)
            {
                moveType = MoveType.Below;
            }
            else
            {
                moveType = MoveType.Into;
            }

            return(moveType);
        }
        private void SwagItemsControl_TreeViewItemLeavePreview(object sender, RoutedEventArgs e)
        {
            TreeViewItemDropEventArgs tviea = (TreeViewItemDropEventArgs)e;

            tviea.TargetItem.BorderThickness = new Thickness(0, 0, 0, 0);
        }
        private void SwagItemsControl_TreeViewItemDrop(object sender, RoutedEventArgs e)
        {
            TreeViewItemDropEventArgs tviea = (TreeViewItemDropEventArgs)e;

            tviea.TargetItem.BorderThickness = new Thickness(0, 0, 0, 0);
            if (!CheckIfValidDrop(tviea))       //Drops are still possible because TreeViewItemDropPreview can be skipped
            {
                return;
            }

            MoveType   moveType   = GetMoveType(tviea);
            SwagOption targetOpt  = (SwagOption)tviea.TargetItem.DataContext;
            SwagOption droppedOpt = (SwagOption)tviea.DroppedItem.DataContext;

            SwagOptionGroup originalDroppedParent   = (SwagOptionGroup)droppedOpt.Parent;
            Boolean         sameParent              = originalDroppedParent == targetOpt.Parent;
            Int32           originalDroppedSequence = droppedOpt.Sequence,
                            currentDroppedSequence  = droppedOpt.Sequence,
                            targetSequence = targetOpt.Sequence,
                            delta          = targetSequence - originalDroppedSequence;

            switch (moveType)
            {
            case MoveType.Above:
            case MoveType.Below:
                if (!sameParent)
                {
                    originalDroppedParent.Children.Remove(droppedOpt);
                    droppedOpt.Sequence = -1;            //reset sequence
                    targetOpt.Parent.Children.Add(droppedOpt);
                    currentDroppedSequence = droppedOpt.Sequence;
                    delta = targetSequence - currentDroppedSequence;
                }

                switch (moveType)
                {
                case MoveType.Above:
                    if (delta != 0)
                    {
                        foreach (SwagOption sibling in targetOpt.Parent.Children)
                        {
                            if (sibling.Sequence >= Math.Min(currentDroppedSequence, targetSequence) && sibling.Sequence < Math.Max(currentDroppedSequence, targetSequence))
                            {
                                sibling.Sequence = (sibling.Sequence + (delta > 0 ? -1 : 1));
                            }
                        }
                        droppedOpt.Sequence = targetSequence + (delta > 0 ? -1 : 0);
                        if (targetOpt.Parent == null)
                        {
                            CollectionViewSource.GetDefaultView(OptionCollection).Refresh();
                        }
                        else
                        {
                            CollectionViewSource.GetDefaultView(targetOpt.Parent.Children).Refresh();
                        }
                    }
                    break;

                case MoveType.Below:
                    if (delta != 0)
                    {
                        foreach (SwagOption sibling in targetOpt.Parent.Children)
                        {
                            if (sibling.Sequence > Math.Min(currentDroppedSequence, targetSequence) && sibling.Sequence <= Math.Max(currentDroppedSequence, targetSequence))
                            {
                                sibling.Sequence = (sibling.Sequence + (delta > 0 ? -1 : 1));
                            }
                        }
                        droppedOpt.Sequence = targetSequence + (delta > 0 ? 0 : 1);
                        if (targetOpt.Parent == null)
                        {
                            CollectionViewSource.GetDefaultView(OptionCollection).Refresh();
                        }
                        else
                        {
                            CollectionViewSource.GetDefaultView(targetOpt.Parent.Children).Refresh();
                        }
                    }
                    break;
                }
                break;

            case MoveType.Into:
                if (tviea.TargetItem.DataContext is SwagOptionGroup grp)
                {
                    originalDroppedParent.Children.Remove(droppedOpt);
                    droppedOpt.Sequence = -1;            //reset sequence
                    grp.Children.Add(droppedOpt);
                }
                break;
            }
        }