title | page_title | description | slug | tags | published | position |
---|---|---|---|---|---|---|
Visual Tree Helpers |
Visual Tree Helpers |
Visual Tree Helpers |
common-visual-tree-helpers |
childrenoftype, parentoftype, visual, tree, helper |
true |
18 |
The UI for {{ site.framework_name }} provides with several extension methods that allows you to enumerate the UI children of an element or find its parents. This way you can get a specific control from the visual tree interact with it.
The methods are available in the ChildrenOfTypeExtensions and ParentOfTypeExtensions static classes. They extend the DependencyObject class so you can call them on any UI element.
important The extension methods would be able to enumerate the children/parents only if the visual tree of the control has been loaded. This is why you should call the methods after the Loaded event of the corresponding control was invoked.
tip To call the method as an extension of DependencyObject you will need to add 'using' directive that points to Telerik.Windows.Controls -
using Telerik.Windows.Controls;
This class exposes several methods that you can use to get the children of an element.
-
ChildrenOfType<T>
ChildrenOfType is a generic method that expects a type that derives from DependencyObject, and it searches the visual tree of the control for elements of the concrete type.
{{region common-visual-tree-helpers-0}} // Using the extension method via the DependencyObject IEnumerable toggleButtons = radExpander.ChildrenOfType();
// Using the method via the static class IEnumerable<RadToggleButton> toggleButtons = ChildrenOfTypeExtensions.ChildrenOfType<RadToggleButton>(radExpander);
{{endregion}}
-
FindChildByType<T>
FindChildByType is a generic method that expects a type that derives from DependencyObject, and it searches the visual tree of the control for an element of the concrete type. The methods returns the first element it founds.
{{region common-visual-tree-helpers-1}} // Using the extension method via the DependencyObject RadToggleButton toggleButton = radExpander.FindChildByType();
// Using the method via the static class RadToggleButton toggleButton = ChildrenOfTypeExtensions.FindChildByType<RadToggleButton>(radExpander);
{{endregion}}
This class exposes several methods that you can use to get the parents of an element.
-
ParentOfType<T>
ParentOfType is a generic method that expects a type that derives from DependencyObject, and it searches the visual tree of the application for parent elements of the concrete type. The method returns the first parent if founds.
{{region common-visual-tree-helpers-2}} // Using the extension method via the DependencyObject RadExpander expander = radToggleButton.ParentOfType();
// Using the method via the static class RadExpander expander = ParentOfTypeExtensions.ParentOfType<RadExpander>(radToggleButton);
{{endregion}}
-
GetParents: This method get all visual parents of the element. The method can be used the same way as in Example 3.
-
GetVisualParent<T>: This method gets the first visual parent of the element. The method can be used the same way as in Example 3.
-
IsAncestorOf: This method checks if an element is a parent of another element.
- [Attached Behaviors]({%slug common-mvvm-attached-behavior%})
- [Delegate Command]({%slug common-mvvm-delegate-command-implementation%})
- [Event to Command Behavior]({%slug common-event-to-command-behavior%})