WPF Window Tree列挙
Window要素を列挙、探索
LogicalTreeHelperクラスのGetChildren()メソッドを使う。
public static IEnumerae LogicalTreeHelper.GetChildren(DependencyObject current)
戻り値は、IEnumerable
パラメーター - current Type: System.Windows.DependencyObject The object from which to start processing the logical tree. This is expected to be either a FrameworkElement or FrameworkContentElement. - 戻り値 Type: System.Collections.IEnumerable The enumerable collection of immediate child objects from the logical tree of the specified object.
public static class DependencyObjectExtension { /// <summary> /// WalkInChildrenメソッドの本体 /// </summary> /// <param name="obj">DependencyObject</param> /// <param name="act">Action</param> private static void Walk(DependencyObject obj, Action<DependencyObject> act) { foreach (var child in LogicalTreeHelper.GetChildren(obj)) { if (child is DependencyObject) { act(child as DependencyObject); Walk(child as DependencyObject, act); } } } /// <summary> /// 子オブジェクトに対してデリゲートを実行する /// </summary> /// <param name="obj">this : DependencyObject</param> /// <param name="act">デリゲート : Action</param> public static void WalkInChildren(this DependencyObject obj, Action<DependencyObject> act) { if (act == null) throw new ArgumentNullException(); Walk(obj, act); } }
親オブジェクト探索
LogicalTreeHelper.GetParent(DepandencyObject)
名前探索
探索方法
- 名前探索 FindName()
- 子を全て列挙 GetChildren()
- ルートウインドウ探索 GetParent()の繰り返し。
- 所属エレメント列挙 GetParent()でルートを見つけてGetChildren()
- 所属エレメント列挙、ルートの名前が分かっている FindName(ルートの名前)でルートを見つけてGetChildren()で列挙。
参考
- http://msdn.microsoft.com/ja-jp/library/ms598654(v=vs.110).aspx
- http://qiita.com/hugo-sb/items/4347214669a99cd2c775
- https://msdn.microsoft.com/ja-jp/library/system.windows.logicaltreehelper(v=vs.110).aspx
- Visual Tree Logical tree - xaml Visual tree - 更に内部の要素を含める http://blog.xin9le.net/entry/2013/10/29/222336