如果把控件的功能视为内容,则可以使用控件模板 ControlTemplate 来控制它的展现;

如果把数据视为内容,则可以使用数据模板 DataTemplate 把数据展示出来;

ControlTemplate 是算法内容的表现形式,一个控件怎样组织其内部结构才让它更符合业务逻辑、让用户操作起来更舒服就是由它来控制的;

DataTemplate 是数据内容的表现形式,一条数据是简单的文本还是图形动画还是其他,由它决定。

1. DataTemplate

1.1 一条柱状图的 DataTemplate 举例

<DataTemplate x:Key="dt">
<Grid>
<StackPanel Orientation="Horizontal">
<Grid>
<Rectangle Width="{Binding Price}" Fill="LightSalmon"/>
<TextBlock Text="{Binding Year}"></TextBlock>
</Grid>
<TextBlock Text="{Binding Price}"></TextBlock>
</StackPanel>
</Grid>
</DataTemplate>

1.2 常用 DataTemplate 的 3 处地方

ContentControl 的 ContentTemplate 属性,给 ContentControl 的内容穿衣服;

ItemsControl 的 ItemTemplate 属性,给 ItemsControl 的数据条目穿衣服;

GridViewColumn 的 CellTemplate 属性,给 GridViewColumn 单元格里的数据穿衣服。

1.3 使用 DataTemplate 实现一个汽车列表和详情

<Window x:Class="WpfAppTemplate.MainWindow"
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"
xmlns:local="clr-namespace:WpfAppTemplate"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="623">
<Window.Resources>
<local:NameToPhotoPathConverter x:Key="nameToPhotoPathConverter"/> <!--集合条目的内容模板-->
<DataTemplate x:Key="carListItemViewTemplate">
<Grid Margin="2">
<StackPanel Orientation="Horizontal">
<Image Height="60" Width="64"
Source="{Binding Name, Converter={StaticResource nameToPhotoPathConverter}}" />
<StackPanel Margin="5 12">
<TextBlock Text="Name" FontWeight="Bold"/>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</StackPanel>
</Grid>
</DataTemplate> <!--详情的内容模板-->
<DataTemplate x:Key="carDetailViewTemplate">
<Border BorderBrush="Black" BorderThickness="1" CornerRadius="6" Margin="2">
<StackPanel Margin="5">
<Image Height="250" Width="400"
Source="{Binding Name, Converter={StaticResource nameToPhotoPathConverter}}" />
<StackPanel Orientation="Horizontal" Margin="5,0">
<TextBlock Text="Name:" FontWeight="Bold" FontSize="19"/>
<TextBlock Text="{Binding Name}" FontSize="19"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="5 0">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Year:" FontWeight="Bold"/>
<TextBlock Text="{Binding Year}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="5 0">
<TextBlock Text="TopSpeed:" FontWeight="Bold"/>
<TextBlock Text="{Binding TopSpeed}"/>
</StackPanel>
</StackPanel>
</StackPanel>
</Border>
</DataTemplate>
</Window.Resources> <Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions> <UserControl x:Name="carDetailControl"
ContentTemplate="{StaticResource carDetailViewTemplate}"
Content="{Binding ElementName=listBoxCar, Path=SelectedItem}" /> <ListBox Grid.Column="1" x:Name="listBoxCar" Margin="2"
ItemTemplate="{StaticResource carListItemViewTemplate}" />
</Grid>
</Window> public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
InitializeList();
} /// <summary>
/// 添加汽车种类
/// </summary>
void InitializeList()
{
List<Car> carList = new List<Car>();
carList.Add(new Car() { Name = "BMW", TopSpeed = "260", Year = "1997" });
carList.Add(new Car() { Name = "Audi", TopSpeed = "240", Year = "1999" });
carList.Add(new Car() { Name = "Toyota", TopSpeed = "120", Year = "2016" });
carList.Add(new Car() { Name = "Volkswagen", TopSpeed = "150", Year = "2000" }); this.listBoxCar.ItemsSource = carList;
}
}

1.4 而如果不用 DataTemplate ,就会显得比较繁琐且冗长

使用 winform 的 Usercontrol 实现一个汽车列表和详情

<!--汽车详情-->
<UserControl x:Class="WpfAppTemplate.UserControls.CarDetailUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfAppTemplate.UserControls"
mc:Ignorable="d" >
<Border BorderBrush="Black" BorderThickness="1" CornerRadius="6" Margin="2">
<StackPanel Margin="5">
<Image x:Name="img" Height="250" Width="400"/> <StackPanel Orientation="Horizontal" Margin="5,0">
<TextBlock Text="Name:" FontWeight="Bold" FontSize="19"/>
<TextBlock x:Name="textBlockName" FontSize="19"/>
</StackPanel> <StackPanel Orientation="Horizontal" Margin="5 0">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Year:" FontWeight="Bold"/>
<TextBlock x:Name="textBlockYear"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="5 0">
<TextBlock Text="TopSpeed:" FontWeight="Bold"/>
<TextBlock x:Name="textBlockTopSpeed"/>
</StackPanel>
</StackPanel> </StackPanel>
</Border>
</UserControl> public partial class CarDetailUserControl : UserControl
{
public CarDetailUserControl()
{
InitializeComponent();
} Car _currentCar;
public Car CurrentCar
{
get { return _currentCar; }
set
{
if (value == null) return;
_currentCar = value;
this.textBlockName.Text = value.Name;
this.textBlockTopSpeed.Text = value.TopSpeed;
this.textBlockYear.Text = value.Year;
string uriStr = string.Format(@"/Resources/Images/Cars/{0}.jpg", value.Name);
this.img.Source = new BitmapImage(new Uri(uriStr, UriKind.Relative));
}
}
} <!--汽车条目-->
<UserControl x:Class="WpfAppTemplate.UserControls.CarListItemUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfAppTemplate.UserControls"
mc:Ignorable="d">
<Grid Margin="2">
<StackPanel Orientation="Horizontal">
<Image x:Name="img" Height="60" Width="64"/>
<StackPanel Margin="5 12">
<TextBlock Text="Name" FontWeight="Bold"/>
<TextBlock x:Name="textBlockName" />
</StackPanel>
</StackPanel>
</Grid>
</UserControl> public partial class CarListItemUserControl : UserControl
{
public CarListItemUserControl()
{
InitializeComponent();
} Car _currentCar;
public Car CurrentCar
{
get { return _currentCar; }
set
{
if (value == null) return; _currentCar = value;
this.textBlockName.Text = value.Name;
string uriStr = string.Format(@"/Resources/Images/Cars/{0}.jpg", value.Name);
this.img.Source = new BitmapImage(new Uri(uriStr, UriKind.Relative));
}
}
} <!--主窗体-->
<Window x:Class="WpfAppTemplate.MainWindow"
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"
xmlns:local="clr-namespace:WpfAppTemplate"
xmlns:localUserControls="clr-namespace:WpfAppTemplate.UserControls"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="623">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions> <localUserControls:CarDetailUserControl x:Name="carDetailControl"/> <ListBox Grid.Column="1" x:Name="listBoxCar" SelectionChanged="listBoxCar_SelectionChanged" Margin="2"/>
</Grid>
</Window> public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
InitializeList();
} /// <summary>
/// 添加汽车种类
/// </summary>
void InitializeList()
{
List<Car> carList = new List<Car>();
carList.Add(new Car() { Name = "BMW", TopSpeed = "260", Year = "1997" });
carList.Add(new Car() { Name = "Audi", TopSpeed = "240", Year = "1999" });
carList.Add(new Car() { Name = "Toyota", TopSpeed = "120", Year = "2016" });
carList.Add(new Car() { Name = "Volkswagen", TopSpeed = "150", Year = "2000" }); // 给每种汽车生成一个 汽车条目控件,作为一条内容添加到汽车列表控件的内容集合
foreach (Car car in carList)
{
CarListItemUserControl control = new CarListItemUserControl();
control.CurrentCar = car;
this.listBoxCar.Items.Add(control);
}
} /// <summary>
/// 当切换汽车时
/// </summary>
/// <param name="sender">listbox</param>
/// <param name="e">SelectionChangedEventArgs, 可以通过 AddedItems 参数取到新选择的项</param>
private void listBoxCar_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
CarListItemUserControl control = e.AddedItems[0] as CarListItemUserControl;
if (control != null)
{
this.carDetailControl.CurrentCar = control.CurrentCar;
}
}
}

WPF 基础 - DataTemplate的更多相关文章

  1. WPF 基础 - DataTemplate 和 ControlTemplate 的关系和应用

    1. 关系 凡是 Template,最后都得作用到 控件 上,这个控件就是 Template 的目标控件(也称模板化控件): DataTemplate 一般是落实在一个 ContentPresente ...

  2. WPF基础到企业应用系列6——布局全接触

    本文转自:http://knightswarrior.blog.51cto.com/1792698/365351 一. 摘要 首先很高兴这个系列能得到大家的关注和支持,这段时间一直在研究Windows ...

  3. WPF 基础到企业应用系列索引

    转自:http://www.cnblogs.com/zenghongliang/archive/2010/07/09/1774141.html WPF 基础到企业应用系列索引 WPF 基础到企业应用系 ...

  4. WPF笔记(1.1 WPF基础)——Hello,WPF!

    原文:WPF笔记(1.1 WPF基础)--Hello,WPF! Example 1-1. Minimal C# WPF application// MyApp.csusing System;using ...

  5. WPF 遍历DataTemplate(获取所有控件)

    原文:WPF 遍历DataTemplate(获取所有控件) 情况1:在设定DataTemplate的Name,并且他是在前台表示时,获取DataTemplate里的指定控件. 方法: http://b ...

  6. C# WPF基础巩固

    时间如流水,只能流去不流回. 学历代表你的过去,能力代表你的现在,学习能力代表你的将来. 学无止境,精益求精. 一.写作目的 做C# WPF开发,无论是工作中即将使用,还是只应付跳槽面试,开发基础是非 ...

  7. WPF基础知识、界面布局及控件Binding(转)

    WPF是和WinForm对应的,而其核心是数据驱动事件,在开发中显示的是UI界面和逻辑关系相分离的一种开放语言.UI界面是在XAML语言环境下开发人员可以进行一些自主设计的前台界面,逻辑关系还是基于c ...

  8. WPF基础知识、界面布局及控件Binding

    WPF是和WinForm对应的,而其核心是数据驱动事件,在开发中显示的是UI界面和逻辑关系相分离的一种开放语言.UI界面是在XAML语言环境下开发人员可以进行一些自主设计的前台界面,逻辑关系还是基于c ...

  9. wpf ListView DataTemplate方式的鼠标悬停和选中更改背景色

    今天使用wpf技术弄一个ListView的时候,由于需求需要,需要ListView显示不同的数据模板,很自然的使用了DataTemplate方式来定义多个数据模板,并在ListView中使用ItemT ...

随机推荐

  1. python-零基础入门-自学笔记

    目录 第一章:计算机基础 1.1 硬件组成 1.2 操作系统分类 1.3 解释型和编译型介绍 第二章:Python入门 2.1 介绍 2.2 python涉及领域 2.2.1 哪些公司有使用Pytho ...

  2. K8S(16)集成实战-使用spinnaker进行自动化部署

    K8s集成实战-使用spinnaker进行自动化部署 1 spinnaker概述和选型 1.1 概述 1.1.1 主要功能 Spinnaker是一个开源的多云持续交付平台,提供快速.可靠.稳定的软件变 ...

  3. woj1018(HDU4384)KING KONG 循环群

    title: woj1018(HDU4384)KING KONG 循环群 date: 2020-03-19 09:43:00 categories: [acm] tags: [acm,woj,数学] ...

  4. ajax全局

    $.ajaxSetup({ complete: function (xhr) { xhr.promise().done(function (json) { if (json.errorNo == &q ...

  5. 深入理解JavaScript中的箭头

    箭头函数可以使我们的代码更加简洁,如下: var sum = (a,b) => a+b; JavaScript 充满了我们需要编写在其他地方执行的小函数的情况. 例如: arr.forEach( ...

  6. Linux 驱动框架---linux 设备

    Linux 设备 Linux驱动中的三大主要基础成员主要是设备,总线和驱动.今天先来从设备开始分析先把设备相关的数据结构放到这里方便后面看到来查,其中有些进行了简单的注释. struct device ...

  7. Inkscape svg彩色图转灰度图

    操作: Ctrl+A 全选所有对象, 然后使用滤镜渲染成灰度图, 至于这个RGB比值, 看个人需求, 标准情况下rgb2gray是0.299 * R + 0.587 * G + 0.114 * B 原 ...

  8. Open API collection

    Open API collection online API https://developer.github.com/v3/ https://developer.github.com/v4 http ...

  9. css 设置多行文本的行间距

    css 设置多行文本的行间距 block element span .ticket-card-info{ line-height:16px; display: inline-block; } .tic ...

  10. webassembly & google

    webassembly & google https://developers.google.com/web/updates/2018/08/wasm-av1 https://develope ...