/ Attempts to find the parent TreeViewItem from the specified event source. Var items = GetItemsRecursively(AssociatedObject) / The list of all items is returned as a convenience to avoid multiple iterations. SetIsItemSelected(treeViewItem, !GetIsItemSelected(treeViewItem)) Public void ToggleSingleItem(TreeViewItem treeViewItem) / Toggles the selection state of the specified tree view item. Public void SelectSingleItem(TreeViewItem treeViewItem) / Selects the specified tree view item, removing any other selections. Toggle isBetweenAnchors when first item is found, and back again when last item is found. If (ReferenceEquals(item, treeViewItem) || ReferenceEquals(item, AnchorItem)) If (ReferenceEquals(AnchorItem, treeViewItem)) Public void SelectMultipleItemsContinuously(TreeViewItem treeViewItem) / Selects a range of consecutive items from the specified tree view item to the anchor (if exists). SelectMultipleItemsContinuously(treeViewItem) Var treeViewItem = FindParentTreeViewItem(e.OriginalSource) Private void OnTreeViewItemMouseUp(object sender, MouseButtonEventArgs e) / Called when a TreeViewItem receives a mouse up event. SelectMultipleItemsContinuously(targetItem) TargetItem = GetRelativeItem(treeViewItem, -1) If (Keyboard.Modifiers = ModifierKeys.Control) TargetItem = GetRelativeItem(treeViewItem, 1) Var treeViewItem = e.OriginalSource as TreeViewItem Private void OnTreeViewItemKeyDown(object sender, KeyEventArgs e) / Called when a TreeViewItem receives a key down event. / Called when the behavior is being detached from its AssociatedObject, but before it hasĪssociatedObject.RemoveHandler(UIElement.KeyDownEvent, new KeyEventHandler(OnTreeViewItemKeyDown)) ĪssociatedObject.RemoveHandler(UIElement.MouseLeftButtonUpEvent, new MouseButtonEventHandler(OnTreeViewItemMouseUp)) / Called after the behavior is attached to an AssociatedObject.ĪssociatedObject.AddHandler(UIElement.KeyDownEvent, new KeyEventHandler(OnTreeViewItemKeyDown), true) ĪssociatedObject.AddHandler(UIElement.MouseLeftButtonUpEvent, new MouseButtonEventHandler(OnTreeViewItemMouseUp), true) #endregion IsItemSelected (TreeViewItem Attached Property) SelectedItems.Remove(treeViewItem.DataContext) SelectedItems.Add(treeViewItem.DataContext) Var selectedItems = behavior?.SelectedItems Var behavior = Interaction.GetBehaviors(treeView).OfType().FirstOrDefault() Var treeView = treeViewItem?.FindVisualAncestor() Private static void OnIsItemSelectedChanged(DependencyObject obj, DependencyPropert圜hangedEventArgs e) / The instance containing the event data. / The dependency object where the value has changed. / Called when the IsItemSelected dependency property has changed. Target.SetValue(IsItemSelectedProperty, value) Public static void SetIsItemSelected(TreeViewItem target, bool value) / Sets the IsItemSelected value on the specified target. Return (bool)target.GetValue(IsItemSelectedProperty) Public static bool GetIsItemSelected(TreeViewItem target) / Gets the IsItemSelected value from the specified target. New FrameworkPropertyMetadata(OnIsItemSelectedChanged)) "IsItemSelected", typeof(bool), typeof(TreeViewMultipleSelectionBehavior), Public static readonly DependencyProperty IsItemSelectedProperty = DependencyProperty.RegisterAttached( / The dependency property definition for the IsItemSelected attached property. #region IsItemSelected (TreeViewItem Attached Property) #endregion AnchorItem (Private Dependency Property) "SelectedItems", typeof(IList), typeof(TreeViewMultipleSelectionBehavior)) Public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register( / The dependency property definition for the SelectedItems property. #region SelectedItems (Public Dependency Property) Public class TreeViewMultipleSelectionBehavior : Behavior / A behavior that extends a with multiple selection capabilities. The living code can be found within CodeMaid’s open source repository here.Īnd just in case, here’s a static snapshot of the code: Here’s a little example of it working in action: Multi-Select Our solution converted this to a behavior and added support for arrow keys and the space bar for more keyboard navigation scenarios. There’s several different solutions out there, and in particular our implementation is largely based on the great work of Cristoph Gattnar available here.Ĭhristoph’s solution utilizes attached properties. To support enhancements like dragging and dropping multiple items at once to CodeMaid‘s Spade tool window, we wanted this capability to multi select. By multiple selection I mean holding down the control key or shift key to select a set of items, similar to behavior available in other controls such as Windows Explorer. The WPF TreeView is very powerful, but out of the box it doesn’t support selecting multiple items at once.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |