<Window x:Class="DateTemplate应用.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:DateTemplate应用" xmlns:collections="clr-namespace:System.Collections;assembly=mscorlib" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <!--DataTemplate--> <DataTemplate DataType="{x:Type local:Unit}"> <Grid> <StackPanel Orientation="Horizontal"> <Grid> <Rectangle Stroke="Yellow" Fill="Orange" Width="{Binding Price}"/> <TextBox Text="{Binding Year}"/> </Grid> <TextBlock Text="{Binding Price}" Margin="5,0"/> </StackPanel> </Grid> </DataTemplate> <!--数据源--> <collections:ArrayList x:Key="ds"> <local:Unit Year="2001" Price="100"/> <local:Unit Year="2002" Price="120"/> <local:Unit Year="2003" Price="140"/> <local:Unit Year="2004" Price="160"/> <local:Unit Year="2006" Price="180"/> <local:Unit Year="2008" Price="200"/> </collections:ArrayList> </Window.Resources> <StackPanel> <ListBox ItemsSource="{StaticResource ds}"/> <ComboBox ItemsSource="{StaticResource ds}"/> </StackPanel> </Window>

以XML数据格式来显示
WPF中的DataTemplate,布布扣,bubuko.com
原文:http://www.cnblogs.com/chenyongblog/p/3569972.html