title | author | description | keywords | dev_langs | ||
---|---|---|---|---|---|---|
DependencyObjectExtensions |
Sergio0694 |
The DependencyObjectExtensions type provides a collection of extensions methods for DependencyObject objects to aid in using the VisualTreeHelper class. |
windows 10, uwp, windows community toolkit, uwp community toolkit, uwp toolkit, Visual Tree, extensions |
|
The DependencyObjectExtensions
type provides a collection of extensions methods for DependencyObject
objects. This class exposes several APIs to aid in using the VisualTreeHelper
class. There are a number of reasons why walking the visual tree might be useful, which are mentioned in the docs.
Platform APIs:
DependencyObjectExtensions
// Include the namespace to access extensions
using Microsoft.Toolkit.Uwp.UI;
// Find a visual descendant control using its name
var control = uiElement.FindDescendant("MyTextBox");
// Find the first visual descendant control of a specified type
control = uiElement.FindDescendant<ListView>();
// Find all visual descendant controls of the specified type.
// We use LINQ here to filter children of a specific type.
using System.Linq;
foreach (var child in uiElement.FindDescendants().OfType<ListViewItem>())
{
// ...
}
// Find the first visual ascendant control using its name
control = uiElement.FindAscendant("MyScrollViewer");
// Find the first visual ascendant control of a specified type
control = uiElement.FindAscendant<ScrollViewer>();
' Include the namespace to access extensions
Imports Microsoft.Toolkit.Uwp.UI.Extensions
' Find a visual descendant control using its name
Dim control = uiElement.FindDescendant("MyTextBox")
' Find the first visual descendant control of a specified type
control = uiElement.FindDescendant(Of ListView)()
' Find all visual descendant controls of the specified type
For Each child In uiElement.FindDescendants().OfType(Of ListViewItem)()
' ...
Next
' Find the first visual ascendant control using its name
control = uiElement.FindAscendant("MyScrollViewer")
' Find the first visual ascendant control of a specified type
control = uiElement.FindAscendant(Of ScrollViewer)()
You can find more examples in the unit tests.