static void OnSelectionModeChanged(object sender, DependencyPropertyChangedEventArgs e) { var TreeView = sender as TreeView; if ((TreeViewSelectionMode)e.NewValue == TreeViewSelectionMode.Single) { var SelectedItems = TreeViewExtensions.GetSelectedItems(TreeView); if (SelectedItems != null && SelectedItems.Count > 1) { TreeView.SelectItem(TreeView.ItemContainerGenerator.ContainerFromItem(SelectedItems[0]).As <TreeViewItem>()); } } }
/// <summary> /// /// </summary> /// <param name="TreeView"></param> /// <param name="TreeViewItem"></param> public static void SelectItemsRandomly(this TreeView TreeView, TreeViewItem TreeViewItem) { TreeViewItemExtensions.SetIsSelected(TreeViewItem, !TreeViewItemExtensions.GetIsSelected(TreeViewItem)); if (TreeViewExtensions.GetStartItem(TreeView) == null || Keyboard.Modifiers == ModifierKeys.Control) { if (TreeViewItemExtensions.GetIsSelected(TreeViewItem)) { TreeViewExtensions.SetStartItem(TreeView, TreeViewItem); } } else { if (TreeViewExtensions.GetSelectedItems(TreeView).Count == 0) { TreeViewExtensions.SetStartItem(TreeView, null); } } }
static void OnIsSelectedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var TreeViewItem = d as TreeViewItem; var TreeView = TreeViewItem.GetVisualParent <TreeView>(); if (TreeViewItem != null && TreeView != null) { var SelectedItems = TreeViewExtensions.GetSelectedItems(TreeView); if (SelectedItems != null) { if (GetIsSelected(TreeViewItem)) { SelectedItems.Add(TreeViewItem.DataContext); } else { SelectedItems.Remove(TreeViewItem.DataContext); } } } }
/// <summary> /// /// </summary> /// <param name="TreeView"></param> /// <param name="TreeViewItem"></param> /// <param name="ShiftControl"></param> public static void SelectItemsContinuously(this TreeView TreeView, TreeViewItem TreeViewItem, bool ShiftControl = false) { var StartItem = TreeViewExtensions.GetStartItem(TreeView); if (StartItem != null) { if (StartItem == TreeViewItem) { SelectItem(TreeView, TreeViewItem); return; } var AllItems = new List <TreeViewItem>(); GetAllItems(TreeView, AllItems); bool IsBetween = false; foreach (var Item in AllItems) { if (Item == TreeViewItem || Item == StartItem) { //Toggle to true if first element is found and back to false if last element is found IsBetween = !IsBetween; //Set boundary element TreeViewItemExtensions.SetIsSelected(Item, true); continue; } if (IsBetween) { TreeViewItemExtensions.SetIsSelected(Item, true); continue; } if (!ShiftControl) { TreeViewItemExtensions.SetIsSelected(Item, false); } } } }
/// <summary> /// /// </summary> /// <param name="TreeView"></param> /// <param name="Item"></param> public static void SelectItem(this TreeView TreeView, TreeViewItem Item) { SelectNone(TreeView); TreeViewItemExtensions.SetIsSelected(Item, true); TreeViewExtensions.SetStartItem(TreeView, Item); }