DependencyObjectExtensions
此 DependencyObjectExtensions
類型會提供 對象的 DependencyObject
擴充方法集合。 這個類別會公開數個 API 來協助使用 類別 VisualTreeHelper
。 執行可視化樹狀結構可能很有用的原因有很多,如檔中所述。
語法
// 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>();
範例
您可以在單元測試中找到更多範例。