internal override void OnMouseDown(MouseButtonEventArgs e)
        {
            base.OnMouseDown(e);

            _ItemForDeferredSelection = null;

            TreeViewExItem item = GetTreeViewItemUnderMouse(e.GetPosition(treeViewEx));

            if (item == null)
            {
                return;
            }
            if (e.ChangedButton != MouseButton.Left && !(e.ChangedButton == MouseButton.Right && item.ContextMenu != null))
            {
                return;
            }
            if (item.IsEditing)
            {
                return;
            }

            // Do not select on MouseDown if part of the selection list
            // To enable drag'n'drop, this selction is done in MouseUp
            if (treeViewEx.SelectedItems.Count > 1 && item.IsSelected)
            {
                _ItemForDeferredSelection = item;
                return;
            }

            SelectSingleItem(item);

            PresentationHelper.ForceFocus(item);
        }
        internal override void OnMouseUp(MouseButtonEventArgs e)
        {
            base.OnMouseUp(e);

            TreeViewExItem item = GetTreeViewItemUnderMouse(e.GetPosition(treeViewEx));

            if (item == null)
            {
                return;
            }
            if (e.ChangedButton != MouseButton.Left && !(e.ChangedButton == MouseButton.Right && item.ContextMenu != null))
            {
                return;
            }
            if (item.IsEditing)
            {
                return;
            }

            // To enable drag'n'drop, this selection is done in MouseUp
            if (ReferenceEquals(_ItemForDeferredSelection, item))
            {
                SelectSingleItem(item);
                _ItemForDeferredSelection = null;
            }
        }
Ejemplo n.º 3
0
        void OnDrop(object sender, DragEventArgs e)
        {
            e.Handled = true;

            TreeViewExItem item = GetTreeViewItemUnderMouse(e.GetPosition(TreeView));
            //if (item == null)
            //{
            //    CleanUpAdorners();
            //    return;
            //}

            CanInsertReturn canInsertReturn = CanInsert(item, e.GetPosition, e.Data);

            if (canInsertReturn != null)
            {
                // insert and return
                TreeView.DropCommand.Execute(new DropParameters(item.ParentTreeViewItem, e.Data, canInsertReturn.Index));
                CleanUpAdorners();
                return;
            }

            // check if drop is possible
            if (CanDrop(item, e.Data))
            {
                // drop and return
                TreeView.DropCommand.Execute(new DropParameters(item, e.Data));
            }

            DragEnd();
        }
Ejemplo n.º 4
0
        public void BringIntoView(object item)
        {
            UpdateLayout();
            TreeViewExItem tvei = GetTreeViewItemFor(item);

            tvei?.BringIntoView();
        }
        public void SelectCurrentBySpace()
        {
            TreeViewExItem item = GetFocusedItem();

            SelectSingleItem(item);
            PresentationHelper.ForceFocus(item);
        }
        protected override void OnKeyUp(KeyEventArgs e)
        {
            base.OnKeyUp(e);
            if (!e.Handled)
            {
                Key key = e.Key;
                switch (key)
                {
                case Key.Left:
                case Key.Right:
                case Key.Up:
                case Key.Down:
                case Key.Add:
                case Key.Subtract:
                case Key.Space:
                    IEnumerable <TreeViewExItem> items = TreeViewElementFinder.FindAll(ParentTreeView, false);
                    TreeViewExItem focusedItem         = items.FirstOrDefault(x => x.IsFocused);

                    if (focusedItem != null)
                    {
                        focusedItem.BringIntoView(new Rect(1, 1, 1, 1));
                    }

                    break;
                }
            }
        }
        private void SelectWithShift(TreeViewExItem item)
        {
            object firstSelectedItem;

            if (lastShiftRoot != null)
            {
                firstSelectedItem = lastShiftRoot;
            }
            else
            {
                if (treeViewEx.SelectedItems.Count > 0)
                {
                    firstSelectedItem = treeViewEx.SelectedItems[0];
                }
                else
                {
                    firstSelectedItem = null;
                }
            }

            TreeViewExItem shiftRootItem = treeViewEx.GetTreeViewItemsFor(new List <object> {
                firstSelectedItem
            }).First();

            List <object> itemsToSelect   = treeViewEx.GetNodesToSelectBetween(shiftRootItem, item).Select(x => x.DataContext).ToList();
            List <object> itemsToUnSelect = ((IEnumerable <object>)treeViewEx.SelectedItems).ToList();

            ModifySelection(itemsToSelect, itemsToUnSelect);
        }
        public DropParameters(TreeViewExItem dropToItem, IDataObject dropData, int index)
        {
            this.DropToItem    = dropToItem;
            this.DropData      = dropData;
            this.InsertAtIndex = index;

            _Content = dropData.GetData(typeof(DragContent)) as DragContent;
        }
Ejemplo n.º 9
0
 private TreeViewExItem GetVisible(TreeViewExItem treeViewExItem)
 {
     if (treeViewExItem.IsVisible)
     {
         return(treeViewExItem);
     }
     return(null);
 }
Ejemplo n.º 10
0
        bool CanDrop(TreeViewExItem item, IDataObject data)
        {
            if (TreeView.DropCommand == null)
            {
                return(false);
            }

            return(TreeView.DropCommand.CanExecute(new DropParameters(item, data)));
        }
Ejemplo n.º 11
0
        private int GetHierarchyLevel()
        {
            TreeViewExItem treeViewItem = ItemsControl.GetItemsOwner(this) as TreeViewExItem;

            if (treeViewItem == null)
            {
                return(0);
            }
            return(treeViewItem.hierachyLevel);
        }
Ejemplo n.º 12
0
        private static void InvalidateMeasure(TreeViewExItem child)
        {
            var itemsPresenter = child.Template.FindName("itemsPresenter", child) as FrameworkElement;

            if (itemsPresenter != null)
            {
                var virtualizingTreePanel = VisualTreeHelper.GetChild(itemsPresenter, 0) as UIElement;
                virtualizingTreePanel.InvalidateMeasure();
            }
        }
Ejemplo n.º 13
0
 private void AddOrInsertItemToInternalChildren(int itemGeneratorIndex, TreeViewExItem child)
 {
     if (itemGeneratorIndex >= InternalChildren.Count)
     {
         base.AddInternalChild(child);
     }
     else
     {
         base.InsertInternalChild(itemGeneratorIndex, child);
     }
 }
        public void SelectLast()
        {
            TreeViewExItem item = TreeViewElementFinder.FindLast(treeViewEx, true);

            if (item != null)
            {
                SelectSingleItem(item);
            }

            PresentationHelper.ForceFocus(item);
        }
 private void ToggleItem(TreeViewExItem item)
 {
     if (treeViewEx.SelectedItems.Contains(item.DataContext))
     {
         ModifySelection(null, item.DataContext);
     }
     else
     {
         ModifySelection(item.DataContext, null);
     }
 }
Ejemplo n.º 16
0
        CanInsertReturn CanInsert(TreeViewExItem item, Func <UIElement, Point> getPositionDelegate, IDataObject data)
        {
            if (TreeView.DropCommand == null)
            {
                return(null);
            }

            if (item == null)
            {
                return(null);
            }
            else
            {
                // get position over element
                Size  size = item.RenderSize;
                Point positionRelativeToItem = getPositionDelegate(item);

                // decide whether to insert before or after item
                bool after = true;
                if (positionRelativeToItem.Y > DragAreaSize)
                {
                    if (size.Height - positionRelativeToItem.Y > DragAreaSize)
                    {
                        return(null);
                    }
                }
                else
                {
                    after = false;
                }

                // get index, where to insert
                TreeViewExItem         parentItem             = item.ParentTreeViewItem;
                ItemContainerGenerator itemContainerGenerator = (parentItem != null)?parentItem.ItemContainerGenerator:TreeView.ItemContainerGenerator;
                int index = itemContainerGenerator.IndexFromContainer(item);
                if (after)
                {
                    // dont allow insertion after item, if item has children
                    if (item.HasItems)
                    {
                        return(null);
                    }
                    index++;
                }

                // ask if insertion is allowed
                if (TreeView.DropCommand.CanExecute(new DropParameters(parentItem, data, index)))
                {
                    return(new CanInsertReturn("", index, !after));
                }
            }

            return(null);
        }
        private static TreeViewExItem GetFirstVirtualizedItem(TreeViewExItem treeViewItem)
        {
            for (int i = 0; i < treeViewItem.Items.Count; i++)
            {
                TreeViewExItem item = treeViewItem.ItemContainerGenerator.ContainerFromIndex(i) as TreeViewExItem;
                if (item != null)
                {
                    return(item);
                }
            }

            return(null);
        }
        /// <summary>
        /// Returns all items in tree recursively. If virtualization is enabled, only realized items are returned.
        /// </summary>
        /// <param name="treeView">The tree.</param>
        /// <param name="visibleOnly">True if only visible items should be returned.</param>
        /// <returns>Returns an enumerable of items.</returns>
        internal static IEnumerable <TreeViewExItem> FindAll(TreeViewEx treeView, bool visibleOnly)
        {
            TreeViewExItem currentItem = FindFirst(treeView, visibleOnly);

            while (currentItem != null)
            {
                if (!visibleOnly || currentItem.IsVisible)
                {
                    yield return(currentItem);
                }
                currentItem = FindNext(currentItem, visibleOnly);
            }
        }
Ejemplo n.º 19
0
        internal bool StopEditing()
        {
            if (_EditedItem == null)
            {
                return(false);
            }

            Keyboard.Focus(_EditedItem);
            _EditedItem.IsEditing = false;
            PresentationHelper.ForceFocus(_EditedItem);
            _EditedItem = null;
            return(true);
        }
Ejemplo n.º 20
0
        internal TreeViewExItem GetNextItem(TreeViewExItem item, List <TreeViewExItem> items)
        {
            int indexOfCurrent = items.IndexOf(item);

            for (int i = indexOfCurrent + 1; i < items.Count; i++)
            {
                if (items[i].IsVisible)
                {
                    return(items[i]);
                }
            }

            return(null);
        }
Ejemplo n.º 21
0
        internal IEnumerable <TreeViewExItem> GetNodesToSelectBetween(TreeViewExItem firstNode, TreeViewExItem lastNode)
        {
            var allNodes   = TreeViewElementFinder.FindAll(this, false).ToList();
            var firstIndex = allNodes.IndexOf(firstNode);
            var lastIndex  = allNodes.IndexOf(lastNode);

            if (firstIndex >= allNodes.Count)
            {
                throw new InvalidOperationException(
                          "First node index " + firstIndex + "greater or equal than count " + allNodes.Count + ".");
            }

            if (lastIndex >= allNodes.Count)
            {
                throw new InvalidOperationException(
                          "Last node index " + lastIndex + " greater or equal than count " + allNodes.Count + ".");
            }

            var nodesToSelect = new List <TreeViewExItem>();

            if (lastIndex == firstIndex)
            {
                return(new List <TreeViewExItem> {
                    firstNode
                });
            }

            if (lastIndex > firstIndex)
            {
                for (int i = firstIndex; i <= lastIndex; i++)
                {
                    if (allNodes[i].IsVisible)
                    {
                        nodesToSelect.Add(allNodes[i]);
                    }
                }
            }
            else
            {
                for (int i = firstIndex; i >= lastIndex; i--)
                {
                    if (allNodes[i].IsVisible)
                    {
                        nodesToSelect.Add(allNodes[i]);
                    }
                }
            }

            return(nodesToSelect);
        }
Ejemplo n.º 22
0
        internal TreeViewExItem GetPreviousItem(TreeViewExItem item, List <TreeViewExItem> items)
        {
            int indexOfCurrent = items.IndexOf(item);

            for (int i = indexOfCurrent - 1; i >= 0; i--)
            {
                if (items[i].IsVisible)
                {
                    return(items[i]);
                }
            }

            return(null);
        }
Ejemplo n.º 23
0
 internal IEnumerable <TreeViewExItem> GetChildren(TreeViewExItem item)
 {
     if (item == null)
     {
         yield break;
     }
     for (int i = 0; i < item.Items.Count; i++)
     {
         TreeViewExItem child = item.ItemContainerGenerator.ContainerFromIndex(i) as TreeViewExItem;
         if (child != null)
         {
             yield return(child);
         }
     }
 }
        public void Dispose()
        {
            if (TreeView != null)
            {
                TreeView.Drop      -= OnDrop;
                TreeView.DragOver  -= OnDragOver;
                TreeView.DragLeave -= OnDragLeave;
            }

            if (_ItemMouseIsOver != null)
            {
                _ItemMouseIsOver.IsCurrentDropTarget = false;
                _ItemMouseIsOver = null;
            }
        }
 public void SelectFromProperty(TreeViewExItem item, bool isSelected)
 {
     // we do not check if selection is allowed, because selecting on that way is no user action.
     // Hopefully the programmer knows what he does...
     if (isSelected)
     {
         treeViewEx.SelectedItems.Add(item.DataContext);
         lastShiftRoot = item.DataContext;
         //FocusHelper.Focus(item);
     }
     else
     {
         treeViewEx.SelectedItems.Remove(item.DataContext);
     }
 }
Ejemplo n.º 26
0
        protected override List <AutomationPeer> GetChildrenCore()
        {
            List <AutomationPeer> children = new List <AutomationPeer>();

            for (int i = 0; i < treeViewExItem.Items.Count; i++)
            {
                TreeViewExItem child = treeViewExItem.ItemContainerGenerator.ContainerFromIndex(i) as TreeViewExItem;
                if (child == null)
                {
                    continue;
                }
                children.Add(new TreeViewExItemAutomationPeer(child, treeViewExAutomationPeer));
            }

            return(children);
        }
Ejemplo n.º 27
0
        List <TreeViewExItem> GetDraggableItems(Point mousePositionRelativeToTree)
        {
            if (TreeView.DragCommand == null)
            {
                return(new List <TreeViewExItem>());
            }

            List <TreeViewExItem> items          = TreeView.GetTreeViewItemsFor(TreeView.SelectedItems).ToList();
            TreeViewExItem        itemUnderMouse = GetTreeViewItemUnderMouse(mousePositionRelativeToTree);

            if (itemUnderMouse == null)
            {
                return(new List <TreeViewExItem>());
            }

            if (items.Contains(itemUnderMouse))
            {
                foreach (var item in items)
                {
                    if (!TreeView.DragCommand.CanExecute(new DragParameters(item)))
                    {
                        // if one item is not draggable, nothing can be dragged
                        return(new List <TreeViewExItem>());
                    }
                }

                return(items);
            }

            //mouse is not over an selected item. We have to check if it is over the content. In this case we have to select and start drag n drop.
            var contentPresenter = itemUnderMouse.Template.FindName("content", itemUnderMouse) as ContentPresenter;

            if (contentPresenter.IsMouseOver)
            {
                if (TreeView.DragCommand.CanExecute(new DragParameters(itemUnderMouse)))
                {
                    return new List <TreeViewExItem> {
                               itemUnderMouse
                    }
                }
                ;
            }

            return(new List <TreeViewExItem>());
        }
Ejemplo n.º 28
0
        public Rect MakeVisible(Visual visual, Rect rectangle)
        {
            if (rectangle.IsEmpty || visual == null || visual == this || !base.IsAncestorOf(visual))
            {
                return(Rect.Empty);
            }

            TreeViewExItem   treeViewExItem = visual as TreeViewExItem;
            FrameworkElement element;

            if (treeViewExItem != null)
            {
                element = treeViewExItem.Template.FindName("border", treeViewExItem) as FrameworkElement;
            }
            else
            {
                element = visual as FrameworkElement;
            }

            var   transform = visual.TransformToAncestor(this);
            Point p         = transform.Transform(new Point(0, 0));
            Rect  rect      = new Rect(p, element.RenderSize);

            if (rect.X < 0)
            {
                SetHorizontalOffset(HorizontalOffset + rect.X);
            }
            else if (treeViewExItem != null && treeViewExItem.ParentTreeView.ActualWidth < rect.X)
            {
                SetHorizontalOffset(HorizontalOffset + rect.X);
            }

            if (rect.Y < 0)
            {
                SetVerticalOffset(VerticalOffset + rect.Y);
            }
            else if (treeViewExItem != null && treeViewExItem.ParentTreeView.ActualHeight < rect.Y + rect.Height)
            {
                // set 5 more, so the next item is realized for sure.
                double verticalOffset = rect.Y + rect.Height + VerticalOffset - treeViewExItem.ParentTreeView.ActualHeight + 5;
                SetVerticalOffset(verticalOffset);
            }

            return(new Rect(HorizontalOffset, VerticalOffset, ViewportWidth, ViewportHeight));
        }
        public void SelectNextFromKey()
        {
            TreeViewExItem item = GetFocusedItem();

            item = TreeViewElementFinder.FindNext(item, true);
            if (item == null)
            {
                return;
            }

            // if ctrl is pressed just focus it, so it can be selected by space. Otherwise select it.
            if (!IsControlKeyDown)
            {
                SelectSingleItem(item);
            }

            PresentationHelper.ForceFocus(item);
        }
        public void SelectPreviousFromKey()
        {
            List <TreeViewExItem> items = TreeViewElementFinder.FindAll(treeViewEx, true).ToList();
            TreeViewExItem        item  = GetFocusedItem();

            item = treeViewEx.GetPreviousItem(item, items);
            if (item == null)
            {
                return;
            }

            // if ctrl is pressed just focus it, so it can be selected by space. Otherwise select it.
            if (!IsControlKeyDown)
            {
                SelectSingleItem(item);
            }

            PresentationHelper.ForceFocus(item);
        }