Sdílet prostřednictvím


DependencyObjectExtensions

Typ DependencyObjectExtensions poskytuje kolekci metod rozšíření pro DependencyObject objekty. Tato třída zveřejňuje několik rozhraní API, která vám pomůžou s používáním VisualTreeHelper třídy. K procházení vizuálního stromu může být užitečné několik důvodů, které jsou uvedené v dokumentaci.

Syntaxe

// 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>();

Příklady

Další příklady najdete v testech jednotek.