WPF TreeView

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,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值