Show / Hide Table of Contents

Class TreeViewExtensions

Provides useful extensions to TreeView and TreeViewItem instances.

Inheritance
System.Object
TreeViewExtensions
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Namespace: System.Windows.Controls
Assembly: OpenSilver.Controls.Toolkit.dll
Syntax
public static class TreeViewExtensions

Fields

| Improve this Doc View Source

IsCheckedProperty

Identifies the IsChecked dependency property.

Declaration
public static readonly DependencyProperty IsCheckedProperty
Field Value
Type Description
DependencyProperty

Methods

| Improve this Doc View Source

ClearSelection(TreeView)

Clear the selection of the TreeView.

Declaration
public static void ClearSelection(this TreeView view)
Parameters
Type Name Description
TreeView view

The TreeView.

Exceptions
Type Condition
System.ArgumentNullException

view is null.

| Improve this Doc View Source

CollapseAll(TreeView)

Collapse all of the items in a TreeView.

Declaration
public static void CollapseAll(this TreeView view)
Parameters
Type Name Description
TreeView view

The TreeView.

Exceptions
Type Condition
System.ArgumentNullException

view is null.

| Improve this Doc View Source

CollapseAllButSelectedPath(TreeView)

Collapse all TreeViewItems except those along the path from the TreeView's SelectedItem to the root of the TreeView.

Declaration
public static void CollapseAllButSelectedPath(this TreeView view)
Parameters
Type Name Description
TreeView view

The TreeView.

Exceptions
Type Condition
System.ArgumentNullException

view is null.

| Improve this Doc View Source

ExpandAll(TreeView)

Expand all of the items in a TreeView.

Declaration
public static void ExpandAll(this TreeView view)
Parameters
Type Name Description
TreeView view

The TreeView.

Exceptions
Type Condition
System.ArgumentNullException

view is null.

| Improve this Doc View Source

ExpandPath(TreeView, Object[])

Expand the given path of items starting from the TreeView's root.

Declaration
public static void ExpandPath(this TreeView view, params object[] items)
Parameters
Type Name Description
TreeView view

The TreeView.

System.Object[] items

The sequence of items corresponding to the path to expand.

Exceptions
Type Condition
System.ArgumentNullException

view is null.

System.ArgumentNullException

items is null.

| Improve this Doc View Source

ExpandPath(TreeViewItem)

Expand a path from the TreeViewItem to the root of the TreeView.

Declaration
public static void ExpandPath(this TreeViewItem item)
Parameters
Type Name Description
TreeViewItem item

The TreeViewItem.

Exceptions
Type Condition
System.ArgumentNullException

item is null.

| Improve this Doc View Source

ExpandPath<T>(TreeView, IEnumerable<T>)

Expand the given path of items starting from the TreeView's root.

Declaration
public static void ExpandPath<T>(this TreeView view, IEnumerable<T> items)
Parameters
Type Name Description
TreeView view

The TreeView.

System.Collections.Generic.IEnumerable<T> items

The sequence of items corresponding to the path to expand.

Type Parameters
Name Description
T

The type of items provided in items.

Exceptions
Type Condition
System.ArgumentNullException

view is null.

System.ArgumentNullException

items is null.

| Improve this Doc View Source

ExpandPath<T>(TreeView, Func<Object, T>, T[])

Expand the given path of items starting from the TreeView's root.

Declaration
public static void ExpandPath<T>(this TreeView view, Func<object, T> comparisonSelector, params T[] items)
Parameters
Type Name Description
TreeView view

The TreeView.

System.Func<System.Object, T> comparisonSelector

A function that takes a TreeViewItem's item and returns a value to compare against elements of the given items. The item itself will be used if comparisonSelector is null.

T[] items

The sequence of items corresponding to the path to expand.

Type Parameters
Name Description
T

The type of items provided in items.

Exceptions
Type Condition
System.ArgumentNullException

view is null.

System.ArgumentNullException

items is null.

| Improve this Doc View Source

ExpandPath<T>(TreeView, Func<Object, T>, IEnumerable<T>)

Expand the given path of items starting from the TreeView's root.

Declaration
public static void ExpandPath<T>(this TreeView view, Func<object, T> comparisonSelector, IEnumerable<T> items)
Parameters
Type Name Description
TreeView view

The TreeView.

System.Func<System.Object, T> comparisonSelector

A function that takes a TreeViewItem's item and returns a value to compare against elements of the given items. The item itself will be used if comparisonSelector is null.

System.Collections.Generic.IEnumerable<T> items

The sequence of items corresponding to the path to expand.

Type Parameters
Name Description
T

The type of items provided in items.

Exceptions
Type Condition
System.ArgumentNullException

view is null.

System.ArgumentNullException

items is null.

| Improve this Doc View Source

ExpandSelectedPath(TreeView)

Expand the path from the SelectedItem to the root of the TreeView.

Declaration
public static void ExpandSelectedPath(this TreeView view)
Parameters
Type Name Description
TreeView view

The TreeView.

Exceptions
Type Condition
System.ArgumentNullException

view is null.

| Improve this Doc View Source

ExpandToDepth(TreeView, Int32)

Expand a specified number of layers in a TreeView.

Declaration
public static void ExpandToDepth(this TreeView view, int depth)
Parameters
Type Name Description
TreeView view

The TreeView.

System.Int32 depth

The number of layers to expand.

Exceptions
Type Condition
System.ArgumentNullException

view is null.

| Improve this Doc View Source

GetCheckedItemsAndContainers(TreeView)

Get the sequence of items and containers with their IsChecked property set to True.

Declaration
public static IEnumerable<KeyValuePair<object, TreeViewItem>> GetCheckedItemsAndContainers(this TreeView view)
Parameters
Type Name Description
TreeView view

The TreeView.

Returns
Type Description
System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<System.Object, TreeViewItem>>

The sequence of items and containers with their IsChecked property set to True.

Exceptions
Type Condition
System.ArgumentNullException

view is null.

| Improve this Doc View Source

GetCheckedItemsAndContainers(TreeView, Boolean)

Get the sequence of items and containers with their IsChecked property set to True.

Declaration
public static IEnumerable<KeyValuePair<object, TreeViewItem>> GetCheckedItemsAndContainers(this TreeView view, bool includeIndeterminate)
Parameters
Type Name Description
TreeView view

The TreeView.

System.Boolean includeIndeterminate

A value indicating whether to include TreeViewItems with an indeterminate IsChecked value.

Returns
Type Description
System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<System.Object, TreeViewItem>>

The sequence of items and containers with their IsChecked property set to True or also set to null if indeterminate values are included.

Exceptions
Type Condition
System.ArgumentNullException

view is null.

| Improve this Doc View Source

GetContainerFromItem(TreeView, Object)

Get the TreeViewItem already created that is used to represent the given item.

Declaration
public static TreeViewItem GetContainerFromItem(this TreeView view, object item)
Parameters
Type Name Description
TreeView view

The TreeView.

System.Object item

The item being represented.

Returns
Type Description
TreeViewItem

The TreeViewItems that represents the given item, or null if no container was found.

Remarks

If multiple TreeViewItems represent the same item, the first item found via a breadth-first search will be used.

Exceptions
Type Condition
System.ArgumentNullException

view is null.

| Improve this Doc View Source

GetContainers(TreeView)

Get the TreeViewItem containers of a TreeView.

Declaration
public static IEnumerable<TreeViewItem> GetContainers(this TreeView view)
Parameters
Type Name Description
TreeView view

The TreeView.

Returns
Type Description
System.Collections.Generic.IEnumerable<TreeViewItem>

The TreeViewItem containers of a TreeView.

Exceptions
Type Condition
System.ArgumentNullException

view is null.

| Improve this Doc View Source

GetContainers(TreeViewItem)

Get the child TreeViewItem containers of a TreeViewItem.

Declaration
public static IEnumerable<TreeViewItem> GetContainers(this TreeViewItem item)
Parameters
Type Name Description
TreeViewItem item

The TreeViewItem.

Returns
Type Description
System.Collections.Generic.IEnumerable<TreeViewItem>

The child TreeViewItem containers of a TreeViewItem.

Exceptions
Type Condition
System.ArgumentNullException

item is null.

| Improve this Doc View Source

GetContainersFromItem(TreeView, Object)

Get the TreeViewItems already created that are used to represent the given item.

Declaration
public static IEnumerable<TreeViewItem> GetContainersFromItem(this TreeView view, object item)
Parameters
Type Name Description
TreeView view

The TreeView.

System.Object item

The item being represented.

Returns
Type Description
System.Collections.Generic.IEnumerable<TreeViewItem>

A sequence of TreeViewItems that represent the given item, or an empty sequence if none were found.

Exceptions
Type Condition
System.ArgumentNullException

view is null.

| Improve this Doc View Source

GetDepth(TreeViewItem)

Gets the depth of a TreeViewItem in its TreeView (using a zero-based index).

Declaration
public static int GetDepth(this TreeViewItem item)
Parameters
Type Name Description
TreeViewItem item

The TreeViewItem.

Returns
Type Description
System.Int32

The depth of a TreeViewItem in its TreeView (using a zero-based index).

Exceptions
Type Condition
System.ArgumentNullException

item is null.

System.ArgumentException

item is not in a TreeView.

| Improve this Doc View Source

GetDescendantContainers(TreeView)

Get the TreeViewItem containers of a TreeView.

Declaration
public static IEnumerable<TreeViewItem> GetDescendantContainers(this TreeView view)
Parameters
Type Name Description
TreeView view

The TreeView.

Returns
Type Description
System.Collections.Generic.IEnumerable<TreeViewItem>

The TreeViewItem containers of a TreeView.

Exceptions
Type Condition
System.ArgumentNullException

view is null.

| Improve this Doc View Source

GetDescendantContainers(TreeViewItem)

Get the descendant TreeViewItem containers of a TreeViewItem.

Declaration
public static IEnumerable<TreeViewItem> GetDescendantContainers(this TreeViewItem item)
Parameters
Type Name Description
TreeViewItem item

The TreeViewItem.

Returns
Type Description
System.Collections.Generic.IEnumerable<TreeViewItem>

The descendant TreeViewItem containers of a TreeViewItem.

Exceptions
Type Condition
System.ArgumentNullException

item is null.

| Improve this Doc View Source

GetDescendantItemsAndContainers(TreeView)

Get the descendant items and TreeViewItem containers of a TreeView.

Declaration
public static IEnumerable<KeyValuePair<object, TreeViewItem>> GetDescendantItemsAndContainers(this TreeView view)
Parameters
Type Name Description
TreeView view

The TreeView.

Returns
Type Description
System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<System.Object, TreeViewItem>>

The descendant items and TreeViewItem containers of a TreeView.

Exceptions
Type Condition
System.ArgumentNullException

view is null.

| Improve this Doc View Source

GetDescendantItemsAndContainers(TreeViewItem)

Get the descendant items and TreeViewItem containers of a TreeViewItem.

Declaration
public static IEnumerable<KeyValuePair<object, TreeViewItem>> GetDescendantItemsAndContainers(this TreeViewItem item)
Parameters
Type Name Description
TreeViewItem item

The TreeViewItem.

Returns
Type Description
System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<System.Object, TreeViewItem>>

The descendant items and TreeViewItem containers of a TreeViewItem.

Exceptions
Type Condition
System.ArgumentNullException

item is null.

| Improve this Doc View Source

GetIsChecked(TreeViewItem)

Gets the value of the IsChecked attached property for a specified TreeViewItem.

Declaration
public static bool? GetIsChecked(this TreeViewItem element)
Parameters
Type Name Description
TreeViewItem element

The TreeViewItem from which the property value is read.

Returns
Type Description
System.Nullable<System.Boolean>

The IsChecked property value for the TreeViewItem.

| Improve this Doc View Source

GetIsLeaf(TreeViewItem)

Gets a value indicating whether the TreeViewItem is a leaf in its TreeView.

Declaration
public static bool GetIsLeaf(this TreeViewItem item)
Parameters
Type Name Description
TreeViewItem item

The TreeViewItem.

Returns
Type Description
System.Boolean

A value indicating whether the TreeViewItem is a leaf in its TreeView.

Exceptions
Type Condition
System.ArgumentNullException

item is null.

| Improve this Doc View Source

GetIsRoot(TreeViewItem)

Gets a value indicating whether the TreeViewItem is a root of its TreeView.

Declaration
public static bool GetIsRoot(this TreeViewItem item)
Parameters
Type Name Description
TreeViewItem item

The TreeViewItem.

Returns
Type Description
System.Boolean

A value indicating whether the TreeViewItem is a root of its TreeView.

Exceptions
Type Condition
System.ArgumentNullException

item is null.

| Improve this Doc View Source

GetItem(TreeViewItem)

Get the item wrapped by this container.

Declaration
public static object GetItem(this TreeViewItem item)
Parameters
Type Name Description
TreeViewItem item

The TreeViewItem.

Returns
Type Description
System.Object

The item wrapped by the container, or null if not found.

Exceptions
Type Condition
System.ArgumentNullException

item is null.

| Improve this Doc View Source

GetItemsAndContainers(TreeView)

Get the items and TreeViewItem containers of a TreeView.

Declaration
public static IEnumerable<KeyValuePair<object, TreeViewItem>> GetItemsAndContainers(this TreeView view)
Parameters
Type Name Description
TreeView view

The TreeView.

Returns
Type Description
System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<System.Object, TreeViewItem>>

The items and TreeViewItem containers of a TreeView.

Exceptions
Type Condition
System.ArgumentNullException

view is null.

| Improve this Doc View Source

GetItemsAndContainers(TreeViewItem)

Get the items and TreeViewItem containers of a TreeViewItem.

Declaration
public static IEnumerable<KeyValuePair<object, TreeViewItem>> GetItemsAndContainers(this TreeViewItem item)
Parameters
Type Name Description
TreeViewItem item

The TreeViewItem.

Returns
Type Description
System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<System.Object, TreeViewItem>>

The items and TreeViewItem containers of a TreeViewItem.

Exceptions
Type Condition
System.ArgumentNullException

item is null.

| Improve this Doc View Source

GetParentItem(TreeView, Object)

Get the item of the parent container for a specified item.

Declaration
public static object GetParentItem(this TreeView view, object item)
Parameters
Type Name Description
TreeView view

The TreeView containing the item.

System.Object item

The child item.

Returns
Type Description
System.Object

The item of the parent container for the specified item, or null if not found.

| Improve this Doc View Source

GetParentTreeView(TreeViewItem)

Get the parent TreeView of a TreeViewItem.

Declaration
public static TreeView GetParentTreeView(this TreeViewItem item)
Parameters
Type Name Description
TreeViewItem item

The TreeViewItem.

Returns
Type Description
TreeView

The parent TreeView if found, otherwise null.

Exceptions
Type Condition
System.ArgumentNullException

item is null.

| Improve this Doc View Source

GetParentTreeViewItem(TreeViewItem)

Get the parent TreeViewItem of a TreeViewItem.

Declaration
public static TreeViewItem GetParentTreeViewItem(this TreeViewItem item)
Parameters
Type Name Description
TreeViewItem item

The TreeViewItem.

Returns
Type Description
TreeViewItem

The parent TreeViewItem if found, otherwise null.

Exceptions
Type Condition
System.ArgumentNullException

item is null.

| Improve this Doc View Source

GetPath(TreeViewItem)

Gets a path of items and TreeViewItem containers from the TreeViewItem to the root of the TreeView.

Declaration
public static IEnumerable<KeyValuePair<object, TreeViewItem>> GetPath(this TreeViewItem item)
Parameters
Type Name Description
TreeViewItem item

The TreeViewItem.

Returns
Type Description
System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<System.Object, TreeViewItem>>

A path of items and TreeViewItem containers from the TreeViewItem to the root of the TreeView.

Exceptions
Type Condition
System.ArgumentNullException

item is null.

| Improve this Doc View Source

GetSelectedContainer(TreeView)

Get the selected TreeViewItem in a TreeView.

Declaration
public static TreeViewItem GetSelectedContainer(this TreeView view)
Parameters
Type Name Description
TreeView view

The TreeView.

Returns
Type Description
TreeViewItem

The selected TreeViewItem, or null if no selected item found.

Exceptions
Type Condition
System.ArgumentNullException

view is null.

| Improve this Doc View Source

GetSelectedPath(TreeView)

Gets the path to the TreeView's selected item.

Declaration
public static IEnumerable<KeyValuePair<object, TreeViewItem>> GetSelectedPath(this TreeView view)
Parameters
Type Name Description
TreeView view

The TreeView.

Returns
Type Description
System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<System.Object, TreeViewItem>>

The path to the TreeView's selected item.

Exceptions
Type Condition
System.ArgumentNullException

view is null.

| Improve this Doc View Source

GetSiblingItemsAndContainers(TreeViewItem)

Get the sibling items and containers of the item.

Declaration
public static IEnumerable<KeyValuePair<object, TreeViewItem>> GetSiblingItemsAndContainers(this TreeViewItem item)
Parameters
Type Name Description
TreeViewItem item

The TreeViewItem.

Returns
Type Description
System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<System.Object, TreeViewItem>>

The sibling items and containers of the item.

| Improve this Doc View Source

SelectItem(TreeView, Object)

Select an item in the TreeView.

Declaration
public static bool SelectItem(this TreeView view, object item)
Parameters
Type Name Description
TreeView view

The TreeView.

System.Object item

The item to select.

Returns
Type Description
System.Boolean

A value indicating whether the item was successfully set as the TreeView's SelectedItem.

Exceptions
Type Condition
System.ArgumentNullException

view is null.

| Improve this Doc View Source

SetIsChecked(TreeViewItem, Nullable<Boolean>)

Sets the value of the IsChecked attached property to a specified TreeViewItem.

Declaration
public static void SetIsChecked(this TreeViewItem element, bool? value)
Parameters
Type Name Description
TreeViewItem element

The TreeViewItem to which the attached property is written.

System.Nullable<System.Boolean> value

The needed IsChecked value.

| Improve this Doc View Source

SetSelectedContainer(TreeView, TreeViewItem)

Sets the selected TreeViewItem of a TreeView.

Declaration
public static void SetSelectedContainer(this TreeView view, TreeViewItem item)
Parameters
Type Name Description
TreeView view

The TreeView.

TreeViewItem item

The TreeViewItem to select.

Exceptions
Type Condition
System.ArgumentNullException

view is null.

  • Improve this Doc
  • View Source