Class TreeViewItem
A row of a TreeView: a SelectorItem that knows its depth, whether it has children and is expanded, and whether it can be selected.
public class TreeViewItem : SelectorItem, INotifyPropertyChanging, IBindingTarget, INotifyPropertyChanged, INotifyFocusChanged, IContainerLayout
- Inheritance
-
TreeViewItem
- Implements
- Inherited Members
- Extension Methods
Remarks
The tree sets every row property each time the row is realized, because a pooled row is reused for another item with only its content's DataContext rebound.
Template parts:
- IndentPartName: any element; its Width is set to the row's indent.
- ExpanderPartName: a TreeViewExpander. It shows ChevronDown when expanded and ChevronRight otherwise, and is transparent and not hit-testable when the row has no children, so leaves keep their alignment.
Fields
ExpanderPartName
The name of the template part that shows the chevron.
public const string ExpanderPartName = "PART_Expander"
Field Value
IndentPartName
The name of the template part whose width indents the row.
public const string IndentPartName = "PART_Indent"
Field Value
Properties
Depth
Gets the row's depth: 0 for a root.
public int Depth { get; }
Property Value
HasChildren
Gets a value indicating whether the row's item has children.
public bool HasChildren { get; }
Property Value
IsExpanded
Gets a value indicating whether the row's item is expanded.
public bool IsExpanded { get; }
Property Value
IsSelectable
Gets a value indicating whether the row's item can be selected.
public bool IsSelectable { get; }
Property Value
Methods
OnApplyTemplate()
Called once, every time Template changes (in either direction - applied, cleared, or switched to a different template), right after Chrome and every decoration/content property have been fully re-wired for the new state.
protected override void OnApplyTemplate()
Remarks
Does nothing by default. A subclass that reaches into its own visual tree by reference - e.g. Slider wiring its drag logic directly to its thumb element - overrides this to call GetTemplateChild<T>(string) and repoint that reference to whatever the new Template (or, when reverting, the fresh default Border) actually provides, instead of hardcoding it once at construction. This is what actually makes a control "template-safe": until a control overrides this appropriately, applying a custom Template to it silently orphans whatever elements its own code still holds direct references to.
OnTap()
Invoked by Canvas when this element (or a descendant) is the target of a completed tap (see Tap). The base implementation does nothing.
protected override void OnTap()
Remarks
Called on the hit-tested element and every one of its ancestors (bubbling, with no "handled" flag in
v1) - so e.g. a Button reacts to a tap on its label TextBlock just as it would to a tap on
its own background.
Events
ExpanderTapped
Occurs when the row's chevron is tapped. The same tap doesn't raise Tapped.
public event EventHandler? ExpanderTapped