Exemple #1
0
        protected override void ProcessKeyDown(KeyRoutedEventArgs args)
        {
            var currentKey = args.Key;

            if (TreeGrid.FlowDirection == FlowDirection.RightToLeft)
            {
                ChangeFlowDirectionKey(ref currentKey);
            }

            var previousCurrentCellIndex = this.CurrentCellManager.CurrentRowColumnIndex;
            var node = TreeGrid.GetNodeAtRowIndex(this.CurrentCellManager.CurrentRowColumnIndex.RowIndex);

            if (currentKey == Key.Left || currentKey == Key.Right)
            {
                if (this.CurrentCellManager.CurrentRowColumnIndex.RowIndex <= this.TreeGrid.GetHeaderIndex())
                {
                    return;
                }

                if (node != null)
                {
                    if (node.IsExpanded && currentKey == Key.Left)
                    {
                        TreeGrid.CollapseNode(node);
                    }
                    else if (!node.IsExpanded && currentKey == Key.Right)
                    {
                        TreeGrid.ExpandNode(node);
                    }
                }
                args.Handled = true;
                return;
            }
            else if (currentKey == Key.Space)
            {
                if (node != null)
                {
                    var isCheckedValue = node.IsChecked;
                    if (isCheckedValue == true)
                    {
                        //node.SetCheckedState(null);
                        node.SetCheckedState(false);
                    }
                    else if (isCheckedValue == null)
                    {
                        node.SetCheckedState(false);
                    }
                    else
                    {
                        node.SetCheckedState(true);
                    }
                }
                args.Handled = true;
                return;
            }
            base.ProcessKeyDown(args);
        }
        protected override void ProcessOnTapped(MouseButtonEventArgs e, RowColumnIndex currentRowColumnIndex)
        {
            if (currentRowColumnIndex.RowIndex <= this.TreeGrid.GetHeaderIndex())
            {
                return;
            }

            var node = TreeGrid.GetNodeAtRowIndex(currentRowColumnIndex.RowIndex);

            if (node != null)
            {
                if (node.IsExpanded)
                {
                    TreeGrid.CollapseNode(node);
                }
                else if (!node.IsExpanded)
                {
                    TreeGrid.ExpandNode(node);
                }
            }
            base.ProcessOnTapped(e, currentRowColumnIndex);
        }
        /// <summary>
        /// Expand nodes to get original parent node
        /// </summary>
        /// <param name="treeNode">Visible parent node</param>
        /// <param name="parentItem">parentItem</param>
        /// <param name="parentItemIndex">parentItemIndex in parentItemCollection</param>
        /// <returns>parent node</returns>
        internal TreeNode ExpandNodeRecursively(TreeNode treeNode, object parentItem, int parentItemIndex)
        {
            TreeNode parentNode = null;

            if (parentItemIndex < 0)
            {
                foreach (var node in treeNode.ChildNodes)
                {
                    if (node.Item == parentItem)
                    {
                        return(node);
                    }
                }
            }
            foreach (var childNode in treeNode.ChildNodes)
            {
                if (childNode.Item == parentItemCollection[parentItemIndex])
                {
                    TreeGrid.ExpandNode(childNode);
                    if (parentItemIndex == 0)
                    {
                        foreach (var node in childNode.ChildNodes)
                        {
                            if (node.Item == parentItem)
                            {
                                return(node);
                            }
                        }
                        break;
                    }
                    parentNode = ExpandNodeRecursively(childNode, parentItem, parentItemIndex - 1);
                    break;
                }
            }
            return(parentNode);
        }
Exemple #4
0
        protected override void ProcessOnDrop(DragEventArgs args, RowColumnIndex rowColumnIndex)
        {
            this.TreeGrid.AutoScroller.AutoScrolling = AutoScrollOrientation.None;
            var dropPosition = GetDropPosition(args, rowColumnIndex);

            if (dropPosition != DropPosition.None && rowColumnIndex.RowIndex != -1)
            {
                var draggedItem = GetDraggedItem(args);
                if (this.TreeGrid.View is TreeGridNestedView)
                {
                    var treeNode = this.TreeGrid.GetNodeAtRowIndex(rowColumnIndex.RowIndex);
                    if (treeNode == null)
                    {
                        return;
                    }
                    var data = treeNode.Item;
                    TreeGrid.SelectionController.SuspendUpdates();
                    var itemIndex = -1;

                    TreeNode parentNode = null;
                    if (dropPosition == DropPosition.DropBelow || dropPosition == DropPosition.DropAbove)
                    {
                        parentNode = treeNode.ParentNode;
                    }
                    else if (dropPosition == DropPosition.DropAsChild)
                    {
                        if (!treeNode.IsExpanded)
                        {
                            TreeGrid.ExpandNode(treeNode);
                        }
                        parentNode = treeNode;
                    }
                    IList sourceCollection = null;
                    if (dropPosition == DropPosition.DropBelow || dropPosition == DropPosition.DropAbove)
                    {
                        if (treeNode.ParentNode != null)
                        {
                            var collection = TreeGrid.View.GetPropertyAccessProvider().GetValue(treeNode.ParentNode.Item, TreeGrid.ChildPropertyName) as IEnumerable;
                            sourceCollection = GetSourceListCollection(collection);
                        }
                        else
                        {
                            sourceCollection = GetSourceListCollection(TreeGrid.View.SourceCollection);
                        }
                        itemIndex = sourceCollection.IndexOf(data);
                        if (dropPosition == DropPosition.DropBelow)
                        {
                            itemIndex += 1;
                        }
                    }
                    else if (dropPosition == DropPosition.DropAsChild)
                    {
                        var collection = TreeGrid.View.GetPropertyAccessProvider().GetValue(data, TreeGrid.ChildPropertyName) as IEnumerable;
                        sourceCollection = GetSourceListCollection(collection);
                        if (sourceCollection == null)
                        {
                            var list = data.GetType().GetProperty(TreeGrid.ChildPropertyName).PropertyType.CreateNew() as IList;
                            if (list != null)
                            {
                                TreeGrid.View.GetPropertyAccessProvider().SetValue(treeNode.Item, TreeGrid.ChildPropertyName, list);
                                sourceCollection = list;
                            }
                        }
                        itemIndex = sourceCollection.Count;
                    }
                    sourceCollection.Insert(itemIndex, draggedItem);
                    TreeGrid.SelectionController.ResumeUpdates();
                    (TreeGrid.SelectionController as TreeGridRowSelectionController).RefreshSelection();
                }
            }
            CloseDragIndicators();
        }
        private void UpdateNodesOnChildPropertyChange(object sender, TreeNode treeNode, PropertyChangedEventArgs e)
        {
            if (SelfRelationUpdateMode.HasFlag(SelfRelationUpdateMode.None))
            {
                return;
            }
            TreeNode newParentNode = null;
            var      childValue    = propertyAccessProvider.GetValue(sender, childPropertyName);

            if (SelfRelationUpdateMode.HasFlag(SelfRelationUpdateMode.MoveOnPropertyChange))
            {
                if (treeNode == null)
                {
                    var rootAdded = AddRootNode(sender, childValue);
                    if (!rootAdded)
                    {
                        newParentNode = FindParentNode(Nodes.RootNodes, childValue);
                        if (newParentNode != null)
                        {
                            if (newParentNode.IsExpanded || newParentNode.ChildNodes.Any())
                            {
                                AddNode(newParentNode, sender, newParentNode.ChildNodes.Count);
                                return;
                            }
                            else
                            {
                                newParentNode.HasChildNodes = true;
                                // If FilterLevel is Root, no need to update HasVisibleChildNodes based on filtering.
                                if (FilterLevel != FilterLevel.Root)
                                {
                                    UpdateHasVisibleChildNodesBasedOnChildItem(newParentNode, sender);
                                }
                                ChangeParentNodeExpander(newParentNode);
                            }
                        }
                    }
                    return;
                }
            }
            var parentNode = treeNode.ParentNode;

            // Changing root node data
            if (parentNode == null)
            {
                var addAsRoot = AddRootNode(sender, childValue, isRoot: true, canAdd: false);
                if (addAsRoot)
                {
                    // no need to do anything
                    return;
                }
            }

            newParentNode = FindParentNode(Nodes.RootNodes, childValue);
            if (newParentNode == parentNode && parentNode != null)
            {
                return;
            }

            if (newParentNode != null)
            {
                // invalid case
                if (IsAncestor(newParentNode, treeNode))
                {
                    // UWP-3144 - When child property and parent property have same value and needs to be added as root, we should remove existing node and add as root.
                    var addAsRoot = AddRootNode(sender, childValue, canAdd: false);
                    if (addAsRoot)
                    {
                        RemoveNode(treeNode);
                        AddNode(null, treeNode, newParentNode.ChildNodes.Count);
                        return;
                    }
                    RemoveNode(parentNode, sender);
                    return;
                }
                else
                {
                    if (this.SelfRelationUpdateMode.HasFlag(SelfRelationUpdateMode.MoveOnPropertyChange) || ((this.SelfRelationUpdateMode.HasFlag(SelfRelationUpdateMode.MoveOnEdit) || this.SelfRelationUpdateMode.HasFlag(SelfRelationUpdateMode.MoveAndExpandOnEdit)) && e == null))
                    {
                        RemoveNode(treeNode);
                        if (newParentNode.IsExpanded || newParentNode.ChildNodes.Any())
                        {
                            AddNode(newParentNode, treeNode, newParentNode.ChildNodes.Count);
                        }
                        else
                        {
                            ResetParentAndLevel(newParentNode, treeNode);
                            newParentNode.HasChildNodes = true;
                            if (CanFilterNode(treeNode))
                            {
                                UpdateParentNodeOnPropertyChange(treeNode, newParentNode);
                            }
                            ChangeParentNodeExpander(newParentNode);
                        }
                        if (e == null && this.SelfRelationUpdateMode.HasFlag(SelfRelationUpdateMode.MoveAndExpandOnEdit))
                        {
                            List <TreeNode> nodes = new List <TreeNode>();
                            tempNode = treeNode;
                            var rootNode = Nodes.GetRootNode(treeNode, ref nodes);
                            for (int i = nodes.Count - 1; i >= 0; i--)
                            {
                                TreeGrid.ExpandNode(nodes[i]);
                            }
                            BringUpdatedItemIntoView(treeNode, childPropertyName);
                            return;
                        }

                        if ((parentNode == null || parentNode.IsExpanded) || newParentNode.IsExpanded)
                        {
                            return;
                        }
                    }
                }
            }
            else
            {
                var parentItem = FindParentItem(childValue);
                if (parentItem == null)
                {
                    if (parentNode == null)
                    {
                        return;
                    }

                    if (this.SelfRelationUpdateMode.HasFlag(SelfRelationUpdateMode.MoveOnPropertyChange) || ((this.SelfRelationUpdateMode.HasFlag(SelfRelationUpdateMode.MoveOnEdit) || this.SelfRelationUpdateMode.HasFlag(SelfRelationUpdateMode.MoveAndExpandOnEdit)) && e == null))
                    {
                        RemoveNode(treeNode);
                        // Check whether node can be added as root.
                        var addAsRoot = AddRootNode(sender, childValue, canAdd: false);
                        if (addAsRoot)
                        {
                            // Add updated tree node as root node
                            AddNode(null, treeNode, Nodes.RootNodes.Count);
                            // While using editing only, need to scroll to updated item.
                            if (e == null)
                            {
                                BringUpdatedItemIntoView(treeNode, childPropertyName);
                            }
                        }
                    }
                    else
                    {
                        RemoveNode(parentNode, sender);
                    }
                    return;
                }
                else
                {
                    var visibleParentNode = FindVisibleParentNode(parentItem);
                    if (IsAncestor(visibleParentNode, treeNode))
                    {
                        RemoveNode(parentNode, sender);
                        return;
                    }
                    else
                    {
                        if (this.SelfRelationUpdateMode.HasFlag(SelfRelationUpdateMode.MoveOnPropertyChange) || ((this.SelfRelationUpdateMode.HasFlag(SelfRelationUpdateMode.MoveOnEdit) || this.SelfRelationUpdateMode.HasFlag(SelfRelationUpdateMode.MoveAndExpandOnEdit)) && e == null))
                        {
                            RemoveNode(treeNode);
                            if (visibleParentNode == null)
                            {
                                return;
                            }
                            if (this.SelfRelationUpdateMode.HasFlag(SelfRelationUpdateMode.MoveAndExpandOnEdit) && e == null)
                            {
                                List <TreeNode> nodes = new List <TreeNode>();
                                tempNode = treeNode;
                                var visibleParentNodeLineCount = visibleParentNode.GetYAmountCache() - 1;
                                nodes.Add(visibleParentNode);
                                var rootNode = Nodes.GetRootNode(visibleParentNode, ref nodes);
                                TreeGrid.TreeGridModel.suspend = true;
                                for (int i = nodes.Count - 1; i >= 0; i--)
                                {
                                    TreeGrid.ExpandNode(nodes[i]);
                                }
                                var orgParentNode = ExpandNodeRecursively(visibleParentNode, parentItem, parentItemCollection.Count - 2);
                                if (orgParentNode != null)
                                {
                                    TreeGrid.ExpandNode(orgParentNode);
                                }
                                TreeGrid.TreeGridModel.suspend = false;
                                foreach (var n in treeNode.ChildNodes)
                                {
                                    n.Level = treeNode.Level + 1;
                                    ResetLevel(n);
                                }
                                tempNode = null;
                                TreeGrid.TreeGridModel.UpdateRows(nodes.FirstOrDefault(), visibleParentNodeLineCount);
                                parentItemCollection.Clear();
                                BringUpdatedItemIntoView(treeNode, childPropertyName);
                            }
                            return;
                        }
                    }
                }
            }
            return;
        }