WPF数据绑定跟DataTemplate简单示例

WPF数据绑定和DataTemplate简单示例
1、数据绑定

数据绑定概述
http://msdn.microsoft.com/zh-cn/library/ms752347.aspx

2、DataTemplate 类
http://msdn.microsoft.com/zh-cn/library/system.windows.datatemplate(v=vs.95).aspx

3、HierarchicalDataTemplate 类
http://msdn.microsoft.com/zh-cn/library/system.windows.hierarchicaldatatemplate.aspx#Y100

先看VO:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace wpfDataBinding.Model
{
    public class Department
    {
        public string Name { get; set; }
        public int Online { get; set; }
        public int Total { get; set; }
    }

    public class DepartmentList : List<Department>
    {
        public DepartmentList()
        {
            Add(new Department() { Name = "部门A", Online = 5, Total = 15 });
            Add(new Department() { Name = "部门B", Online = 10, Total = 30 });
            Add(new Department() { Name = "部门C", Online = 2, Total = 5 });
            Add(new Department() { Name = "部门D", Online = 10, Total = 20 });
        }
    }
}
 使用它:
<Window x:Class="wpfDataBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:wpfDataBinding.Model"
        Title="MainWindow" Height="350" Width="525">
    
    <Window.Resources>
        <local:DepartmentList x:Key="departmentList"/>
        <DataTemplate x:Key="treeViewItemStyle" DataType="Department">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=Name}"/>
                <TextBlock Margin="5,0,5,0">
                    <TextBlock.Text>
                       <MultiBinding StringFormat="[{0}/{1}]">
                           <Binding Path="Online"/>
                           <Binding Path="Total"/>
                       </MultiBinding>
                    </TextBlock.Text>
                </TextBlock>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    
    <Grid>
        <TreeView Name="TreeRoot" 
                  ItemsSource="{Binding Source={StaticResource departmentList}}"
                  ItemTemplate="{StaticResource treeViewItemStyle}"/>
    </Grid>
</Window>
OK,仅做备忘,抛砖引玉!
善用DataTemplate,用数据支撑起Template的灵魂吧...