Silverlight TreeView通过HierarchicalDataTemplate直接绑定多级数据源

<UserControl xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
           xmlns:Common
="clr-namespace:System.Windows;assembly=System.Windows.Controls"
           x:Class
="SilverlightApplication1.Page1"
           xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:d
="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:mc
="http://schemas.openxmlformats.org/markup-compatibility/2006"
           mc:Ignorable
="d"
           d:DesignWidth
="640" d:DesignHeight="480"
          
>
   
<UserControl.Resources>
       
<Common:HierarchicalDataTemplate x:Key="TreeNode" ItemsSource="{Binding Path=ChildList}">
           
<TextBlock Text="{Binding Path=Name}"/>
       
</Common:HierarchicalDataTemplate>
   
</UserControl.Resources>
   
<StackPanel x:Name="LayoutRoot" Loaded="LayoutRoot_Loaded" Orientation="Horizontal">
       
<controls:TreeView x:Name="myTreeView"   ItemTemplate="{StaticResource TreeNode}">
       
</controls:TreeView>
   
</StackPanel>
</UserControl>

注:代码是自己在网上找的,就是想记下来方便以后查看

.cs myTreeView.ItemSource = list;//list为数据对象列表

ChildList为数据对象中子列表属性

同样适用的还有TreeGrid哦

posted @ 2012-05-23 17:05  321程序员  Views(1064)  Comments(1)    收藏  举报