如深入浅出WPF中的描述,DataTemplate为数据的外衣,ControlTemplate为控件的外衣。ControlTemplate控制控件的样式,DataTemplate控制数据显示的样式,DataTemplate是ControlTemplate的一部分。本文介绍DataTemplate与ControlTemplate结合使用的方法,其关键在于ContentPresenter,它是DataTemplate的树根,代表DataTemplate的实例。

场景

自定义Button,使其显示当前页与总页数,当页码变化时自动更新。

实现步骤
  1. 自定义Button.ControlTemplate;
  2. 自定义Button.ContentTemplate;
  3. 创建数据类;
  4. 创建ViewModel类;
  5. 绑定。
示例代码:
// xaml
<UserControl.Resources>
<viewmodel:TextViewModel x:Key="TestViewModel"/>
</UserControl.Resources> <Grid DataContext="{StaticResource TextViewModel}">
<Button Width="120" Height="50" Content="{Binding PageInfo}">
<Button.Template>
<ControlTemplate TargetType="Button">
<ContentPresenter/>
</ControlTemplate>
</Button.Template>
<Button.ContentTemplate>
<DataTemplate>
<TextBlock Width="{TemplateBinding Width}" TextAlignment="Center"
FontSize="36" FontFamily="微软雅黑" Foreground="#ffffff">
<Run Text="{Binding CurrentPage}"/>
<Run Text="/"/>
<Run Text="{Binding TotalPages}"/>
</TextBlock>
</DataTemplate>
</Button.ContentTemplate>
</Button>
</Grid> // 数据类
public class PageInfo : ViewModelBase
{
public PageInfo(string currentPage, string totalPages)
{
this.CurrentPage = currentPage;
this.TotalPages = totalPages;
} public string CurrentPage
{
get { return currentPage; }
set
{
currentPage = value;
OnPropertyChanged("CurrentPage");
}
} public string TotalPages
{
get { return totalPages; }
set
{
totalPages = value;
OnPropertyChanged("TotalPages");
}
} private string currentPage;
private string totalPages;
} // viewmodel类
public class TestViewModel : ViewModelBase
{
public TextViewModel()
{
PageInfo = new PageInfo("1", "1");
} public PageInfo PageInfo
{
get { return pageInfo; }
set { pageInfo = value; }
} // 其它逻辑 private PageInfo pageInfo;
}

WPF -- DataTemplate与ControlTemplate结合使用的更多相关文章

  1. WPF DataTemplate與ControlTemplate

    一. 前言     什麼是DataTemplate? 什麼是ControlTemplate? 在stackoverflow有句簡短的解釋 "A DataTemplate, therefore ...

  2. WPF Template模版之DataTemplate与ControlTemplate【一】

    WPF Template模版之DataTemplate与ControlTemplate[一] 标签: Wpf模版 2015-04-19 11:52 510人阅读 评论(0) 收藏 举报  分类: -- ...

  3. WPF Template模版之DataTemplate与ControlTemplate的关系和应用【二】

    1. DataTemplate和ControlTemplate的关系 学习过DataTemplate和ControlTemplate,你应该已经体会到,控件只是数据的行为和载体,是个抽象的概念,至于它 ...

  4. [WPF]如何使用代码创建DataTemplate(或者ControlTemplate)

    1. 前言 上一篇文章([UWP]如何使用代码创建DataTemplate(或者ControlTemplate))介绍了在UWP上的情况,这篇文章再稍微介绍在WPF上如何实现. 2. 使用Framew ...

  5. 【转】WPF Template模版之DataTemplate与ControlTemplate的关系和应用(二)

    1. DataTemplate和ControlTemplate的关系 学习过DataTemplate和ControlTemplate,你应该已经体会到,控件只是数据的行为和载体,是个抽象的概念,至于它 ...

  6. DataTemplate和ControlTemplate的关系

    DataTemplate和ControlTemplate的关系(转载自haiziguo) 一.ContentControl中的DataTemplate 在开始之前,我们先去看一下ContentCont ...

  7. WPF中的ControlTemplate(控件模板)(转)

    原文地址 http://www.cnblogs.com/zhouyinhui/archive/2007/03/28/690993.html WPF中的ControlTemplate(控件模板)     ...

  8. [UWP]如何使用代码创建DataTemplate(或者ControlTemplate)

    1. 前言 在UWP中DataTemplate是一个十分重要的功能,并且几乎无处不在,例如DataGrid中的DataGridTemplateColumn: <controls:DataGrid ...

  9. WPF中的ControlTemplate(控件模板)

    原文:WPF中的ControlTemplate(控件模板) WPF中的ControlTemplate(控件模板)                                             ...

随机推荐

  1. 2019 Multi-University Training Contest 1 Path(最短路+最小割)

    题意:给你n个点 m条边 现在你能够堵住一些路 问怎样能让花费最少且让1~n走的路比最短路的长度要长 思路:先跑一边最短路 建一个最短路图 然后我们跑一边最大流求一下最小割即可 #include &l ...

  2. HHKB Programming Contest 2020【ABCE】

    比赛链接:https://atcoder.jp/contests/hhkb2020/tasks A - Keyboard 代码 #include <bits/stdc++.h> using ...

  3. HDU5739 Fantasia【点双连通分量 割点】

    HDU5739 Fantasia 题意: 给出一张\(N\)个点的无向图\(G\),每个点都有权值\(w_i\),要求计算\(\sum_{i=1}^{N}i\cdot G_i % 1e9+7\) 其中 ...

  4. 2017-2018 ACM-ICPC German Collegiate Programming Contest (GCPC 2017)(9/11)

    $$2017-2018\ ACM-ICPC\ German\ Collegiate\ Programming\ Contest (GCPC 2017)$$ \(A.Drawing\ Borders\) ...

  5. Codeforces Gym-102219 2019 ICPC Malaysia National E. Optimal Slots(01背包+输出路径)

    题意:给你一个体积为\(T\)的背包,有\(n\)个物品,每个物品的价值和体积都是是\(a_{i}\),求放哪几个物品使得总价值最大,输出它们,并且输出价值的最大值. 题解:其实就是一个01背包输出路 ...

  6. Codeforces Round #498 (Div. 3) D. Two Strings Swaps (思维)

    题意:给你两个长度相同的字符串\(a\)和\(b\),你可以将相同位置上的\(a\)和\(b\)的字符交换,也可以将\(a\)或\(b\)中某个位置和对应的回文位置上的字符交换,这些操作是不统计的,你 ...

  7. CS224--1:语言模型和词向量

    参考: https://www.cnblogs.com/pinard/p/7243513.html https://blog.csdn.net/cindy_1102/article/details/8 ...

  8. Bing壁纸-20200416

  9. python 表达式

    运算符 参考 https://www.runoob.com/python3/python3-basic-operators.html & https://www.runoob.com/pyth ...

  10. 如何在github实现 github pages (暨个人blog) websites for you and your projects.

    http://xgqfrms.github.io/xgqfrms/ 步骤如下: 1.GitHub Pages https://pages.github.com/ 2.Generate a site, ...