在C#代码中展开WPF TreeView控件

/// <summary>
    /// </summary> 
    public static class ExtensionMethods
    {
        public static void ExpandAll(this System.Windows.Controls.TreeView treeView)
        {
            ExpandAllItems(treeView);
        }
 
        private static void ExpandAllItems(ItemsControl control)
        {
            if (control == null)
            {
                return;
            }
 
            foreach (Object item in control.Items)
            {
                System.Windows.Controls.TreeViewItem treeItem = control.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
                if (treeItem == null || !treeItem.HasItems)
                {
                    continue;
                }
 
                treeItem.IsExpanded = true;
                treeItem.UpdateLayout();
                ExpandAllItems(treeItem as ItemsControl);
            }
        }
    }
posted @ 2013-07-22 22:04  AndrewYu  阅读(749)  评论(0编辑  收藏  举报