VisualTreeHelper.GetChild(DependencyObject, Int32) メソッド

定義

指定されたインデックスを使用して、ビジュアル ツリーを調べることで、指定されたオブジェクトの特定の子オブジェクトを取得します。

public:
 static DependencyObject ^ GetChild(DependencyObject ^ reference, int childIndex);
 static DependencyObject GetChild(DependencyObject const& reference, int const& childIndex);
public static DependencyObject GetChild(DependencyObject reference, int childIndex);
function getChild(reference, childIndex)
Public Shared Function GetChild (reference As DependencyObject, childIndex As Integer) As DependencyObject

パラメーター

reference
DependencyObject

子コレクションを保持する オブジェクト。

childIndex
Int32

int

参照子コレクション内の要求された子オブジェクトのインデックス。

戻り値

childIndex によって参照される子オブジェクト。

ビジュアル ツリー内から特定の型の子要素の一覧をコピーできるユーティリティ関数の例を次に示します。 基本的なトラバーサル メソッド GetChildrenCount と GetChild を使用します。 再帰を使用して、中間コンテナー内の入れ子のレベルに関係なく要素を見つけることができます。 また、Type の一致としてサブタイプを考慮するように型比較を拡張する System.ReflectionIsSubclassOf 拡張メソッドも使用します

internal static void FindChildren<T>(List<T> results, DependencyObject startNode)
  where T : DependencyObject
{
    int count = VisualTreeHelper.GetChildrenCount(startNode);
    for (int i = 0; i < count; i++)
    {
        DependencyObject current = VisualTreeHelper.GetChild(startNode, i);
        if ((current.GetType()).Equals(typeof(T)) || (current.GetType().GetTypeInfo().IsSubclassOf(typeof(T))))
        {
            T asType = (T)current;
            results.Add(asType);
        }
        FindChildren<T>(results, current);
    }
}

適用対象