static void GetIndices(IEnumerable <MenuItem> items, string[] headerTexts, int[] indices, int index)
        {
            int i = 0;

            foreach (var e in items)
            {
                if (e.Header.ToString() == headerTexts[index])
                {
                    indices[index] = i;
                    if (index == indices.Length - 1)
                    {
                        return;
                    }

                    //次のメニューを開く
                    IInvokeProvider invoker = new MenuItemAutomationPeer(e);
                    invoker.Invoke();

                    foreach (var popup in SearcherInTarget.ByType <Popup>(TreeUtilityInTarget.VisualTree(e)))
                    {
                        GetIndices(SearcherInTarget.ByType <MenuItem>(TreeUtilityInTarget.VisualTree(popup.Child)), headerTexts, indices, index + 1);
                        return;
                    }
                    break;
                }
                i++;
            }
            throw new NotSupportedException(ResourcesLocal3.Instance.ErrorNotFoundItem);
        }
        static int GetItemCount(MenuItem item)
        {
            IInvokeProvider invoker = new MenuItemAutomationPeer(item);

            invoker.Invoke();
            foreach (var p in SearcherInTarget.ByType <Popup>(TreeUtilityInTarget.VisualTree(item)))
            {
                int count = 0;
                foreach (var e in SearcherInTarget.ByType <MenuItem>(TreeUtilityInTarget.VisualTree(p.Child)))
                {
                    count++;
                }
                return(count);
            }
            return(0);
        }
        static int[] GetIndices(UIElement target, string[] headerTexts)
        {
            Clean cleaner = null;

            try
            {
                var   menu    = OpenMenu(target, out cleaner);
                int[] indices = new int[headerTexts.Length];
                GetIndices(SearcherInTarget.ByType <MenuItem>(TreeUtilityInTarget.VisualTree(menu)), headerTexts, indices, 0);
                return(indices);
            }
            finally
            {
                if (cleaner != null)
                {
                    cleaner();
                }
            }
        }
        internal static int GetItemCount(UIElement target, bool openByKey)
        {
            Clean cleaner = null;

            try
            {
                var menu  = OpenMenu(target, openByKey, out cleaner);
                int count = 0;
                foreach (var e in SearcherInTarget.ByType <MenuItem>(TreeUtilityInTarget.VisualTree(menu)))
                {
                    count++;
                }
                return(count);
            }
            finally
            {
                if (cleaner != null)
                {
                    cleaner();
                }
            }
        }
        /// <summary>
        /// Search by Type from DependencyObject collection.
        /// </summary>
        /// <param name="collection">DependencyObject collection.</param>
        /// <param name="typeFullName">Target type.</param>
        /// <returns>Hit elements.</returns>
#else
        /// <summary>
        /// タイプから要素を検索。
        /// </summary>
        /// <param name="collection">DependencyObjectのコレクション。</param>
        /// <param name="typeFullName">検索対象のタイプ。</param>
        /// <returns>ヒットした要素。</returns>
#endif
        public static IEnumerable <DependencyObject> ByType(this IEnumerable <DependencyObject> collection, string typeFullName)
        {
            return(SearcherInTarget.ByType(collection, typeFullName));
        }
        /// <summary>
        /// Search by Type from DependencyObject collection.
        /// </summary>
        /// <typeparam name="T">Target type.</typeparam>
        /// <param name="collection">DependencyObject collection.</param>
        /// <returns>Hit elements.</returns>
#else
        /// <summary>
        /// タイプから要素を検索。
        /// </summary>
        /// <typeparam name="T">検索対象のタイプ。</typeparam>
        /// <param name="collection">DependencyObjectのコレクション。</param>
        /// <returns>ヒットした要素。</returns>
#endif
        public static IEnumerable <T> ByType <T>(this IEnumerable <DependencyObject> collection) where T : DependencyObject
        {
            return(SearcherInTarget.ByType <T>(collection));
        }