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

 XAML页面代码:

<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(数据源)

posted @ 2012-08-27 22:05  cicadachina  阅读(149)  评论(0)    收藏  举报