1、不同于Windows Forms,当前WPF版本没有提供一个直接的方法可以把TreeView控件所有的节点都展开。一般来说,在WPF中有两种方法可以实现这个功能。第一种方法就像下面例子一样使用样式展开所有节点:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="ControlTest2.TreeViewTest" Width="500"> <Window.Resources> <XmlDataProvider x:Key="treeData" XPath="*"> <x:XData> <Items Name="Items" xmlns=""> <Item1/> <Item2> <Item22/> <Item12/> <Item13> <Item131/> <Item131/> </Item13> </Item2> </Items> </x:XData> </XmlDataProvider> <HierarchicalDataTemplate ItemsSource="{Binding XPath=child::*}" x:Key="template"> <TextBlock Name="textBlock" Text="{Binding Name}"/> </HierarchicalDataTemplate> </Window.Resources> <WrapPanel> <TreeView ItemTemplate="{StaticResource template}" ItemsSource="{Binding Source={StaticResource treeData}}"> <TreeView.ItemContainerStyle> <!--Using style setter to set the TreeViewItem.IsExpanded property to true, this will be applied to all TreeViweItems when they are generated--> <Style TargetType="{x:Type TreeViewItem}"> <Setter Property="IsExpanded" Value="True"/> </Style> </TreeView.ItemContainerStyle> </TreeView> </WrapPanel> </Window>
参考:http://social.msdn.microsoft.com/Forums/zh-CN/wpfzhchs/thread/857fdaa9-5c67-4e0a-a1fd-037f72577c76