0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ビジュアルツリーの子孫要素を列挙する

Posted at

やりたいこと

ビジュアルツリーから子孫要素をIEnumerableとして取得する。

実装

VisualTreeHelperクラスを使えば、子孫要素の取得自体はできるのだが使いにくい。そこで子孫要素を列挙する拡張メソッドをDependencyObjectに作成する。

拡張メソッド
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Media;

namespace Sample {
    public static class DependencyObjectExtensions {
        public static IEnumerable<DependencyObject> Children(this DependencyObject obj) {
            if (obj == null)
                throw new ArgumentNullException(nameof(obj));

            return Enumerable.Range(0, VisualTreeHelper.GetChildrenCount(obj))
                .Select(i => VisualTreeHelper.GetChild(obj, i))
                .Where(x => x != null);
        }

        public static IEnumerable<DependencyObject> Descendants(this DependencyObject obj) {
            if (obj == null)
                throw new ArgumentNullException(nameof(obj));

            foreach (var child in obj.Children()) {
                yield return child;
                foreach (var grandChild in child.Descendants())
                    yield return grandChild;
            }
        }
        
        public static IEnumerable<T> Descendants<T>(this DependencyObject obj) where T : DependencyObject {
            if (obj == null)
                throw new ArgumentNullException(nameof(obj));

            return obj.Descendants().OfType<T>();
        }
    }
}

使い方

var headers = grid.Descendants<DataGridColumnHeader>();
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?