DataTemplate顧名思義,就是數據模板,用來指定數據的表現形式。這對於ItemsControl類的控件尤其有用,可以改變列表項的外觀,更具有表現能力。
例如

<Grid> <Grid.Resources> <src:Customers x:Key="customers"/> </Grid.Resources> <ListBox ItemsSource="{StaticResource customers}" Width="350" Margin="0,5,0,10"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Padding="5,0,5,0" Text="{Binding FirstName}" /> <TextBlock Text="{Binding LastName}" /> <TextBlock Text=", " /> <TextBlock Text="{Binding Address}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid>
上例中通過指定ListBox.ItemTemplate屬性來定義子項的顯示格式,如果不是列表項,可以通過控件的ContentTemplate屬性來指定應用哪個數據模板。
使用起來比較簡單,大概分為三步:
1.如果有些屬性不是可以直接使用,需要轉換的,寫轉換器,實現IValueConverter接口
2.定義好轉換器之后,需要實例化,指定給需要的綁定
3.定義模板,也就是需要的顯示格式
4.應用模板,把定義好的模板指定到目標控件的ContentTemplate或者ItemTemplate
看一個完整的例子
1.用來封裝的實體類和轉換器

class Car
{
public string Automaker { get; set; }
public string Name { get; set; }
public string Year { get; set; }
public string TopSpeed { get; set; }
}
//廠商名稱轉化為Logo圖片
public class AutomakerToLogoPathConverter:IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string uriStr = string.Format(@"images/{0}.jpg",value.ToString());
return new BitmapImage(new Uri(uriStr, UriKind.Relative));
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
//汽車名稱轉化為圖像
public class NameToPhotoPathConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string uriStr = string.Format(@"images/{0}.jpg", value.ToString());
return new BitmapImage(new Uri(uriStr, UriKind.Relative));
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
2.模板及窗體

<Window x:Class="TempaletPractise.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TempaletPractise" Title="汽車展覽" Height="350" Width="623"> <Window.Resources> <!--Converter--> <local:AutomakerToLogoPathConverter x:Key="a2l"></local:AutomakerToLogoPathConverter> <local:NameToPhotoPathConverter x:Key="n2p"></local:NameToPhotoPathConverter> <!--Data Template for Detail View--> <DataTemplate x:Key="carDetailViewTemplate"> <Border BorderBrush="Black" BorderThickness="1" CornerRadius="6"> <StackPanel Margin="5"> <Image Width="400" Height="250" Source="{Binding Name, Converter={StaticResource n2p}}"></Image> <StackPanel Orientation="Horizontal" Margin="5,0"> <TextBlock Text="Name:" FontWeight="Bold" FontSize="20"></TextBlock> <TextBlock Text="{Binding Name}" FontSize="20" Margin="5,0"></TextBlock> </StackPanel> <StackPanel Orientation="Horizontal" Margin="5,0"> <TextBlock Text="Automaker" FontWeight="Bold"></TextBlock> <TextBlock Text="{Binding Automaker}" Margin="5,0"></TextBlock> <TextBlock Text="Year" FontWeight="Bold"></TextBlock> <TextBlock Text="{Binding Year}" Margin="5,0"></TextBlock> <TextBlock Text="Top Speed" FontWeight="Bold"></TextBlock> <TextBlock Text="{Binding TopSpeed}" Margin="5,0"></TextBlock> </StackPanel> </StackPanel> </Border> </DataTemplate> <!--DataTemplate for Item View--> <DataTemplate x:Key="carListItemViewTemplate"> <Grid Margin="2"> <StackPanel Orientation="Horizontal"> <Image Source="{Binding Automaker,Converter={StaticResource a2l}}" Grid.RowSpan="3" Width="64" Height="64"></Image> <StackPanel Margin="5,0"> <TextBlock Text="{Binding Name}" FontSize="16" FontWeight="Bold"></TextBlock> <TextBlock Text="{Binding Year}" FontSize="14"></TextBlock> </StackPanel> </StackPanel> </Grid> </DataTemplate> </Window.Resources> <!--窗體的內容--> <StackPanel Orientation="Horizontal" Margin="5"> <UserControl ContentTemplate="{StaticResource carDetailViewTemplate}" Content="{Binding SelectedItem, ElementName=listBoxCars}"></UserControl> <ListBox x:Name="listBoxCars" Width="180" Margin="5,0" ItemTemplate="{StaticResource carListItemViewTemplate}"></ListBox> </StackPanel> </Window>
3.指定數據源

private void InitialCarList() { List<Car> cars = new List<Car>() { new Car(){ Automaker="CADILLAC",Name="CADILLAC1",Year="1990",TopSpeed="340"}, new Car(){ Automaker="Ferrari",Name="Ferrari1",Year="1991",TopSpeed="350"}, new Car(){ Automaker="LAMBORGHINI",Name="LAMBORGHINI1",Year="1992",TopSpeed="360"}, new Car(){ Automaker="PORSCHE",Name="PORSCHE1",Year="1993",TopSpeed="370"} }; this.listBoxCars.ItemsSource = cars; }
參考 MSDN 《深入淺出WPF》第11章
ps.今天看到園子編輯推薦的一則新聞,Silverlight開始邊緣化,從官方就開始不推了,不知道WPF的命運會怎樣,在微軟的大船上,只能跟着起伏,不過今天能從ASP.NET轉WPF,明天也能從WPF轉其他技術,我們從來就不怕這個,畢竟我們學過,想過,用過,他幫助過我們,每種技術都代表了一種思想,技術可以沒落,思想卻沉淀下來了。