Class TreeView
Displays a hierarchical tree of data.
public class TreeView : ItemsControl, INotifyPropertyChanged, IDataContextProvider, ILogical, IThemeVariantHost, IResourceHost, IResourceNode, IStyleHost, ISetLogicalParent, ISetInheritanceParent, ISupportInitialize, IStyleable, INamed, IInputElement, IDataTemplateHost, ISetterValue, IChildIndexProvider, ICustomKeyboardNavigation
- Inheritance
-
TreeView
- Implements
- Inherited Members
- Extension Methods
Constructors
TreeView()
public TreeView()
Fields
AutoScrollToSelectedItemProperty
Defines the AutoScrollToSelectedItem property.
public static readonly StyledProperty<bool> AutoScrollToSelectedItemProperty
Field Value
SelectedItemProperty
Defines the SelectedItem property.
public static readonly DirectProperty<TreeView, object?> SelectedItemProperty
Field Value
SelectedItemsProperty
Defines the SelectedItems property.
public static readonly DirectProperty<TreeView, IList> SelectedItemsProperty
Field Value
SelectionModeProperty
Defines the SelectionMode property.
public static readonly StyledProperty<SelectionMode> SelectionModeProperty
Field Value
Properties
AutoScrollToSelectedItem
Gets or sets a value indicating whether to automatically scroll to newly selected items.
public bool AutoScrollToSelectedItem { get; set; }
Property Value
Remarks
This property is of limited use with TreeView as it will only scroll to realized items. To scroll to a non-expanded item, you need to ensure that its ancestors are expanded.
ItemContainerGenerator
Gets the TreeItemContainerGenerator for the tree view.
public TreeItemContainerGenerator ItemContainerGenerator { get; }
Property Value
SelectedItem
Gets or sets the selected item.
public object? SelectedItem { get; set; }
Property Value
Remarks
Note that setting this property only currently works if the item is expanded to be visible.
To select non-expanded nodes use Selection.SelectedIndex
.
SelectedItems
Gets or sets the selected items.
public IList SelectedItems { get; set; }
Property Value
SelectionMode
Gets or sets the selection mode.
public SelectionMode SelectionMode { get; set; }
Property Value
Methods
CollapseSubTree(TreeViewItem)
Collapse the specified TreeViewItem all descendent TreeViewItem s.
public void CollapseSubTree(TreeViewItem item)
Parameters
item
TreeViewItemThe item to collapse.
ContainerForItemPreparedOverride(Control, object?, int)
Called when a container has been fully prepared to display an item.
protected override void ContainerForItemPreparedOverride(Control container, object? item, int index)
Parameters
container
ControlThe container control.
item
objectThe item being displayed.
index
intThe index of the item being displayed.
Remarks
This method will be called when a container has been fully prepared and added to the logical and visual trees, but may be called before a layout pass has completed. It is called immediately before the ContainerPrepared event is raised.
CreateContainerForItemOverride(object?, int, object?)
Creates or a container that can be used to display an item.
protected override Control CreateContainerForItemOverride(object? item, int index, object? recycleKey)
Parameters
Returns
ExpandSubTree(TreeViewItem)
Expands the specified TreeViewItem all descendent TreeViewItems.
public void ExpandSubTree(TreeViewItem item)
Parameters
item
TreeViewItemThe item to expand.
GetContainerFromEventSource(object)
Tries to get the container that was the source of an event.
protected TreeViewItem? GetContainerFromEventSource(object eventSource)
Parameters
eventSource
objectThe control that raised the event.
Returns
- TreeViewItem
The container or null if the event did not originate in a container.
GetRealizedTreeContainers()
public IEnumerable<Control> GetRealizedTreeContainers()
Returns
NeedsContainerOverride(object?, int, out object?)
Determines whether the specified item can be its own container.
protected override bool NeedsContainerOverride(object? item, int index, out object? recycleKey)
Parameters
item
objectThe item to check.
index
intThe index of the item.
recycleKey
objectWhen the method returns, contains a key that can be used to locate a previously recycled container of the correct type, or null if the item cannot be recycled. If the item is its own container then by definition it cannot be recycled, so
recycleKey
shoud be set to null.
Returns
- bool
true if the item needs a container; otherwise false if the item can itself be used as a container.
OnCreateAutomationPeer()
Returns a new, type-specific AutomationPeer implementation for the control.
protected override AutomationPeer OnCreateAutomationPeer()
Returns
- AutomationPeer
The type-specific AutomationPeer implementation.
OnGotFocus(GotFocusEventArgs)
Called before the GotFocus event occurs.
protected override void OnGotFocus(GotFocusEventArgs e)
Parameters
e
GotFocusEventArgsThe event args.
OnKeyDown(KeyEventArgs)
Handles directional navigation within the ItemsControl.
protected override void OnKeyDown(KeyEventArgs e)
Parameters
e
KeyEventArgsThe key events.
OnPointerPressed(PointerPressedEventArgs)
Called before the PointerPressed event occurs.
protected override void OnPointerPressed(PointerPressedEventArgs e)
Parameters
e
PointerPressedEventArgsThe event args.
SelectAll()
Selects all items in the TreeView.
public void SelectAll()
Remarks
Note that this method only selects nodes currently visible due to their parent nodes being expanded: it does not expand nodes.
TreeContainerFromItem(object)
public Control? TreeContainerFromItem(object item)
Parameters
item
object
Returns
TreeItemFromContainer(Control)
public object? TreeItemFromContainer(Control container)
Parameters
container
Control
Returns
UnselectAll()
Deselects all items in the TreeView.
public void UnselectAll()
UpdateSelectionFromContainer(Control, bool, bool, bool, bool)
Updates the selection for an item based on user interaction.
protected void UpdateSelectionFromContainer(Control container, bool select = true, bool rangeModifier = false, bool toggleModifier = false, bool rightButton = false)
Parameters
container
ControlThe container.
select
boolWhether the item should be selected or unselected.
rangeModifier
boolWhether the range modifier is enabled (i.e. shift key).
toggleModifier
boolWhether the toggle modifier is enabled (i.e. ctrl key).
rightButton
boolWhether the event is a right-click.
UpdateSelectionFromEventSource(object, bool, bool, bool, bool)
Updates the selection based on an event that may have originated in a container that belongs to the control.
protected bool UpdateSelectionFromEventSource(object eventSource, bool select = true, bool rangeModifier = false, bool toggleModifier = false, bool rightButton = false)
Parameters
eventSource
objectThe control that raised the event.
select
boolWhether the container should be selected or unselected.
rangeModifier
boolWhether the range modifier is enabled (i.e. shift key).
toggleModifier
boolWhether the toggle modifier is enabled (i.e. ctrl key).
rightButton
boolWhether the event is a right-click.
Returns
- bool
True if the event originated from a container that belongs to the control; otherwise false.
Events
SelectionChanged
Occurs when the control's selection changes.
public event EventHandler<SelectionChangedEventArgs>? SelectionChanged