Silverlight TreeView HierarchicalDataTemplate

摘要:Silverlight TreeView HierarchicalDataTemplate

<UserControl x:Class="TreeViewBinding.Page"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:kit="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls"
             Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <kit:TreeView x:Name="treeTest">
            <kit:TreeView.ItemTemplate>
                <kit:HierarchicalDataTemplate ItemsSource="{Binding CatData}">
                    <TextBlock Margin="0" Text="{Binding Name, Mode=OneWay}" FontSize="12" />
                </kit:HierarchicalDataTemplate>
            </kit:TreeView.ItemTemplate>
            <!--
            <kit:TreeView.ItemContainerStyle>
                <Style TargetType="kit:TreeViewItem">
                    <Setter Property="IsExpanded" Value="{Binding Expanded}"/>
                </Style>
            </kit:TreeView.ItemContainerStyle>
            -->
        </kit:TreeView>
    </Grid>
</UserControl>
 

 

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Microsoft.Windows.Controls;

namespace TreeViewBinding
{
    public partial class Page : UserControl
    {
        ObservableCollection<Category> m_listCategory  = new ObservableCollection<Category>();

        public ObservableCollection<Category> CategoryList
        {
            get { return m_listCategory; }
            set { m_listCategory = value; }
        }

        public Page()
        {
            InitializeComponent();

            treeTest.ItemsSource = m_listCategory;

            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0,0,5);
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            if (m_listCategory.Count <= 0)
            {
                m_listCategory.Add(new Category() { Name = "Test1", CatData = new ObservableCollection<Category>() });
                m_listCategory.Add(new Category() { Name = "Test2", CatData = new ObservableCollection<Category>() });
                m_listCategory.Add(new Category() { Name = "Test3", CatData = new ObservableCollection<Category>()});
                return;
            }
            Category cat0 = m_listCategory[0];
            if (cat0.CatData.Count <= 0)
            {
                cat0.CatData.Add(new Category() { Name = "111", CatData = null });
                cat0.CatData.Add(new Category() { Name = "222", CatData = null });
                cat0.CatData.Add(new Category() { Name = "333", CatData = null });
                cat0.CatData.Add(new Category() { Name = "444", CatData = null });
                cat0.CatData.Add(new Category() { Name = "555", CatData = null });
                return;
            }
            Category cat1 = m_listCategory[1];
            if (cat1.CatData.Count <= 0)
            {
                cat1.CatData.Add(new Category() { Name = "2111", CatData = null });
                cat1.CatData.Add(new Category() { Name = "2222", CatData = null });
                cat1.CatData.Add(new Category() { Name = "2333", CatData = null });
                return;
            }
            Category cat2 = m_listCategory[2];
            if (cat2.CatData.Count <= 0)
            {
                cat2.CatData.Add(new Category() { Name = "32111", CatData = null });
                cat2.CatData.Add(new Category() { Name = "32222", CatData = null });
                cat2.CatData.Add(new Category() { Name = "32333", CatData = null });
                return;
            }

        }
    }

    public class Category
    {
        public string Name { get; set; }
        public ObservableCollection<Category> CatData { get; set; }
    }
}