且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WPF TreeView

更新时间:2022-09-16 15:47:53

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



本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2011/11/14/2248326.html,如需转载请自行联系原作者