Ejemplo n.º 1
0
        private InsertIndicator CreateInsertIndicator(VerticalAlignment alignment)
        {
            var insertIndicator = new InsertIndicator();

            if (alignment == System.Windows.VerticalAlignment.Top)
            {
                var transform = new TranslateTransform(0d, -3.5d);
                insertIndicator.RenderTransform = transform;
            }
            else if (alignment == System.Windows.VerticalAlignment.Bottom)
            {
                var transform = new TranslateTransform(15d, 3d);
                insertIndicator.RenderTransform = transform;
            }

            insertIndicator.VerticalAlignment = alignment;
            insertIndicator.IsHitTestVisible  = false;
            return(insertIndicator);
        }
Ejemplo n.º 2
0
        private void ShowInsertIndicator(Grid panel, InsertIndicator insertIndicator)
        {
            var columnCtn = panel.ColumnDefinitions.Count;
            var rowCtn    = panel.RowDefinitions.Count;

            if (columnCtn > 1)
            {
                Grid.SetColumnSpan(insertIndicator, columnCtn);
            }

            if (rowCtn > 1)
            {
                Grid.SetRowSpan(insertIndicator, rowCtn);
            }

            panel.Children.Add(insertIndicator);
            this._lastPanel           = panel;
            this._lastInsertIndicator = insertIndicator;
        }
Ejemplo n.º 3
0
        private void treeView_DragOver(object sender, DragEventArgs e)
        {
            try
            {
                if (_viewmodel.IsStandardDocument == false || this._isEditable || this._itemBorders == null)
                {
                    e.Handled = true;
                    e.Effects = DragDropEffects.None;
                    return;
                }

                ///如果当前位置位于InsertIndicator上那么直接忽略
                ///之前使用GetClosetParent[T]判断是否位于InsertIndicator上
                ///但是由于GetClosetParent[T]过于耗时,替换方法
                var type = e.OriginalSource.GetType();
                if (type.GetProperty("Name") != null)
                {
                    dynamic objDynamic = e.OriginalSource;
                    var     onname     = objDynamic.Name;
                    if (onname == "InsertIndicatorEllipse" || onname == "InsertIndicatorGrid")
                    {
                        e.Handled = true;
                        return;
                    }
                }

                this.RemoveLastInsertIndicator();
                this.HideIsDragInto();
                var relativePosition = default(Point);
                var border           = this.GetClosetParentWithName <Grid>(e.OriginalSource as DependencyObject, "ItemBd");
                if (border != null)
                {
                    ///拖动到一个ItemBd上面,包括每个元素的图片文本以及后面的空白的位置
                    relativePosition = e.GetPosition(border);
                    if (relativePosition.Y <= 5)
                    {
                        ///如果在鼠标在ItemBd的Y坐标<=5,说明在元素的上沿
                        ///插入指示要放置在[上一个]元素
                        ///[上一个]元素也有两种情况:
                        ///1,如果当前ItemBd所在的TreeViewItem是父节点的第一个元素,那么[上一个]元素是父节点
                        ///2,如果当前ItemBd所在的TreeViewItem不是父节点的第一个元素,那么[上一个]元素是上一个兄弟节点
                        ///统一在拖动开始前获取所有ItemBd,然后在DragOver时获取上一个节点
                        var prepanel = this._itemBorders.TakeWhile(x => x != border).LastOrDefault();
                        if (prepanel != null)
                        {
                            ///上一个节点不为空
                            this.ShowIsDragInto(prepanel);
                            this._dragInfo.Parent          = prepanel;
                            this._dragInfo.PreviousBrother = null;
                            if (!IsDescent(this._dragInfo.NodeViewModel, prepanel.DataContext as NodeViewModel))
                            {
                                ///不是拖动到自己或者自己的子节点内
                                this.ShowInsertIndicator(prepanel, System.Windows.VerticalAlignment.Bottom);
                            }
                            else
                            {
                                this.ShowDisabledInsertIndicator(prepanel, System.Windows.VerticalAlignment.Bottom);
                                e.Handled = true;
                                e.Effects = DragDropEffects.None;
                                return;
                            }
                        }
                        else
                        {
                            ///上一个节点为空,说明当前位置为第一个节点,插入到当前ItemBd的Top位置
                            this.ShowInsertIndicator(border, System.Windows.VerticalAlignment.Top);

                            this._dragInfo.Parent          = null;
                            this._dragInfo.PreviousBrother = null;
                        }
                    }
                    else
                    {
                        ///否则,插入到当前ItemBd的Bottom位置
                        this.ShowIsDragInto(border);

                        this._dragInfo.Parent          = border;
                        this._dragInfo.PreviousBrother = null;
                        if (!IsDescent(this._dragInfo.NodeViewModel, border.DataContext as NodeViewModel))
                        {
                            this.ShowInsertIndicator(border, System.Windows.VerticalAlignment.Bottom);
                        }
                        else
                        {
                            this.ShowDisabledInsertIndicator(border, System.Windows.VerticalAlignment.Bottom);
                            e.Handled = true;
                            e.Effects = DragDropEffects.None;
                            return;
                        }
                    }
                }
                else
                {
                    ///拖动到TreeViewItemPanel上面
                    var actualPanel = GetActualTreeviewPanel(e, e.OriginalSource as DependencyObject);
                    if (actualPanel != null)
                    {
                        var fstchildgrid = FindVisualChildren <Grid>(actualPanel).FirstOrDefault();
                        if (fstchildgrid != null)
                        {
                            var insertIndicator = new InsertIndicator();
                            var transform       = new TranslateTransform(fstchildgrid.Margin.Left + 14, 3.5d);
                            insertIndicator.RenderTransform   = transform;
                            insertIndicator.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
                            insertIndicator.IsHitTestVisible  = false;
                            this.ShowInsertIndicator(actualPanel, insertIndicator);

                            var parentPanel = this.GetClosetParentWithName <Grid>(VisualTreeHelper.GetParent(actualPanel), "TreeViewItemPanel");
                            if (parentPanel != null)
                            {
                                this.ShowIsDragInto(parentPanel);

                                this._dragInfo.Parent          = parentPanel;
                                this._dragInfo.PreviousBrother = actualPanel;

                                if (IsDescent(this._dragInfo.NodeViewModel, parentPanel.DataContext as NodeViewModel))
                                {
                                    insertIndicator.IsEnable = false;
                                    e.Handled = true;
                                    e.Effects = DragDropEffects.None;
                                    return;
                                }
                            }
                            else
                            {
                                this._dragInfo.Parent          = null;
                                this._dragInfo.PreviousBrother = actualPanel;
                            }
                        }
                    }
                }


                e.Handled = true;
                e.Effects = DragDropEffects.Move;
            }
            catch (Exception)
            {
            }
        }