2011年6月17日金曜日

【Silverlight】 全コントロール一覧を取得するには

IEnumerable objList
    = from v in this.GetVisualTree()
      select v;
using System;
using System.Windows;
using System.Windows.Media;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Linq;

namespace Extensions
{
    // DependencyObjectクラスの拡張メソッド
    public static class DependencyObjectExtensions
    {
        // 子要素一覧を取得します。
        public static IEnumerable 
            GetVisualTree(this DependencyObject obj)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                var child = VisualTreeHelper.GetChild(obj, i);
                yield return child;

                foreach (var children in GetVisualTree(child))
                {
                    yield return children;
                }
            }
        }
    }
}

0 件のコメント:

コメントを投稿