/// <summary> /// Select the given element, if supported; element is valid if supports <see cref="Selector.SetIsSelected(DependencyObject, bool)"/> or is <see cref="TreeViewItem"/>. /// </summary> /// <param name="Value"></param> /// <param name="IsSelected"></param> public static void Select(this DependencyObject Value, bool IsSelected) { if (Value.IsAny(typeof(ListBoxItem), typeof(DataGridRow))) { Selector.SetIsSelected(Value, IsSelected); } else if (Value is TreeViewItem) { TreeViewItemExtensions.SetIsSelected(Value as TreeViewItem, IsSelected); } else { throw new ArgumentException("Object doesn't support selection."); } }
/// <summary> /// /// </summary> /// <param name="Control"></param> public static void SelectNone(this ItemsControl Control) { if (Control != null) { for (int i = 0; i < Control.Items.Count; i++) { var Item = Control.ItemContainerGenerator.ContainerFromIndex(i) as TreeViewItem; if (Item != null) { TreeViewItemExtensions.SetIsSelected(Item, false); SelectNone(Item); } } } }
/// <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); } } }
/// <summary> /// /// </summary> /// <param name="Control"></param> /// <param name="Item"></param> public static void Select(this ItemsControl Control, object Item) { if (Control != null) { foreach (var i in Control.Items) { var j = (TreeViewItem)Control.ItemContainerGenerator.ContainerFromItem(i); if (Item == i) { if (j is TreeViewItem) { TreeViewItemExtensions.SetIsSelected(j as TreeViewItem, true); } } else { TreeViewItemExtensions.SetIsSelected(j as TreeViewItem, false); Select(j, Item); } } } }
/// <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); }