private void SelectWithShift(TreeViewExItem item) { object firstSelectedItem; if (lastShiftRoot != null) { firstSelectedItem = lastShiftRoot; } else { // Get the first item in the SelectedItems that is also bound to the Tree. firstSelectedItem = treeViewEx.SelectedItems.Cast <object>().FirstOrDefault((x) => { return(treeViewEx.GetTreeViewItemFor(x) != null); }); } if (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 = treeViewEx.SelectedItems.Cast <object>().ToList(); ModifySelection(itemsToSelect, itemsToUnSelect); } else { // Fall-back to sigle selection List <object> itemsToUnSelect = treeViewEx.SelectedItems.Cast <object>().ToList(); if (itemsToUnSelect.Contains(item.DataContext)) { itemsToUnSelect.Remove(item.DataContext); } ModifySelection(item.DataContext, itemsToUnSelect); } }
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; } } }
public void SelectCurrentBySpace() { TreeViewExItem item = GetFocusedItem(); SelectSingleItem(item); FocusHelper.Focus(item); }
internal override void OnMouseDown(MouseButtonEventArgs e) { base.OnMouseDown(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; } // ToggleItem or SelectWithShift if (IsControlKeyDown || (IsShiftKeyDown)) { SelectSingleItem(item); FocusHelper.Focus(item); return; } // begin click if (selectItem == null) { selectItem = item; selectPosition = e.GetPosition(TreeView); selectButton = e.ChangedButton; } }
public void BringIntoView(object item) { UpdateLayout(); TreeViewExItem tvei = GetTreeViewItemFor(item); tvei.BringIntoView(); }
private TreeViewExItem GetVisible(TreeViewExItem treeViewExItem) { if (treeViewExItem.IsVisible) { return(treeViewExItem); } return(null); }
private int GetHierarchyLevel() { TreeViewExItem treeViewItem = ItemsControl.GetItemsOwner(this) as TreeViewExItem; if (treeViewItem == null) { return(0); } return(treeViewItem.hierachyLevel); }
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(); } }
public void SelectLast() { TreeViewExItem item = TreeViewElementFinder.FindLast(treeViewEx, true); if (item != null) { SelectSingleItem(item); } FocusHelper.Focus(item); }
private void ToggleItem(TreeViewExItem item) { if (treeViewEx.SelectedItems.Contains(item.DataContext)) { ModifySelection(null, item.DataContext); } else { ModifySelection(item.DataContext, null); } }
private void AddOrInsertItemToInternalChildren(int itemGeneratorIndex, TreeViewExItem child) { if (itemGeneratorIndex >= InternalChildren.Count) { base.AddInternalChild(child); } else { base.InsertInternalChild(itemGeneratorIndex, child); } }
internal void StopEditing() { if (editedItem == null) { return; } Keyboard.Focus(editedItem); editedItem.IsEditing = false; FocusHelper.Focus(editedItem); editedItem = null; }
internal void UnSelectByRectangle(TreeViewExItem item) { if (!treeViewEx.CheckSelectionAllowed(item.DataContext, false)) { return; } treeViewEx.SelectedItems.Remove(item.DataContext); if (item.DataContext == lastShiftRoot) { lastShiftRoot = 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); } }
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); }
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); }
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); }
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); }
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); } }
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); } } }
internal override void OnMouseUp(MouseButtonEventArgs e) { base.OnMouseUp(e); // click if (selectItem != null && e.ChangedButton == selectButton) { // select item SelectSingleItem(selectItem); FocusHelper.Focus(selectItem); // end click selectItem = null; } }
internal override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if (selectItem != null) { // detect drag var dragDiff = selectPosition - e.GetPosition(TreeView); if ((Math.Abs(dragDiff.X) > SystemParameters.MinimumHorizontalDragDistance || Math.Abs(dragDiff.Y) > SystemParameters.MinimumVerticalDragDistance)) { // abort click selectItem = null; } } }
private Rect GetPositionOf(TreeViewExItem treeViewItem) { FrameworkElement item = (FrameworkElement)treeViewItem.Template.FindName("border", treeViewItem); if (item == null) { throw new InvalidOperationException("Could not get content of item"); } Point p = item.TransformToAncestor(TreeView).Transform(new Point()); double itemLeft = p.X + TreeView.ScrollViewer.ContentHorizontalOffset; double itemTop = p.Y + TreeView.ScrollViewer.ContentVerticalOffset; return(new Rect(itemLeft, itemTop, item.ActualWidth, item.ActualHeight)); }
internal override void OnMouseDown(MouseButtonEventArgs e) { // begin click if (!selectPosition.HasValue && !TreeView.IsVirtualizing) // TODO: Virtual Elements still don't work with Selection. { TreeViewExItem item = GetTreeViewItemUnderMouse(e.GetPosition(TreeView)); if (item != null) { var contentPresenter = item.Template.FindName("content", item) as ContentPresenter; if (!contentPresenter.IsMouseOver) { selectPosition = GetMousePositionRelativeToContent(); dragButton = e.ChangedButton; } } } }
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); } FocusHelper.Focus(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); } FocusHelper.Focus(item); }
private void Debug(TreeViewExItem item, string message) { if (item != null) { System.Diagnostics.Debug.Write(String.Format("{0,15}--", item.DataContext)); int indent = GetHierarchyLevel(); for (int i = 0; i < indent; i++) { System.Diagnostics.Debug.Write("--"); } } else { System.Diagnostics.Debug.Write(" --"); } System.Diagnostics.Debug.Write(">"); System.Diagnostics.Debug.WriteLine(message); }
private void SelectSingleItem(TreeViewExItem item) { // selection with SHIFT is not working in virtualized mode. Thats because the Items are not visible. // Therefor the children cannot be found/selected. if (IsShiftKeyDown && treeViewEx.SelectedItems.Count > 0 && !treeViewEx.IsVirtualizing) { SelectWithShift(item); } else if (IsControlKeyDown) { ToggleItem(item); } else { List <object> itemsToUnSelect = treeViewEx.SelectedItems.Cast <object>().ToList(); if (itemsToUnSelect.Contains(item.DataContext)) { itemsToUnSelect.Remove(item.DataContext); } ModifySelection(item.DataContext, itemsToUnSelect); } }
internal static TreeViewExItem FindNext(TreeViewExItem treeViewItem, bool visibleOnly) { // find first child if (treeViewItem.IsExpanded || !visibleOnly) { TreeViewExItem item = GetFirstVirtualizedItem(treeViewItem); if (item != null) { if (item.IsEnabled) { if (visibleOnly && !item.IsVisible) { return(FindNext(item, visibleOnly)); } else { return(item); } } else { return(FindNext(item, visibleOnly)); } } } // find next sibling TreeViewExItem sibling = FindNextSiblingRecursive(treeViewItem) as TreeViewExItem; if (sibling == null) { return(null); } if (!visibleOnly || sibling.IsVisible) { return(sibling); } return(null); }