如深入浅出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. validate插件

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. Codeforces Round #635 (Div. 2)

    Contest Info Practice Link Solved A B C D E F 4/6 O O Ø  Ø     O 在比赛中通过 Ø 赛后通过 ! 尝试了但是失败了 - 没有尝试 Sol ...

  3. Codeforces Round #628 (Div. 2) C. Ehab and Path-etic MEXs(树,思维题)

    题意: 给有 n 个点的树的 n-1 条边从 0 到 n-2 编号,使得任意两点路径中未出现的最小数最小的方案. 思路: 先给所有度为 1 的点所在边编号,之后其他点可以随意编排. #include ...

  4. Buy the Ticket HDU - 1133 大数dp

    题意: 演唱会门票售票处,那里最开始没有零钱.每一张门票是50元,人们只会拿着100元和50元去买票,有n个人是拿着50元买票,m个人拿着100元去买票. n+m个人按照某个顺序按序买票,如果一个人拿 ...

  5. hdu5564 Clarke and digits

    Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission ...

  6. UESTC 1218 Pick The Sticks

    Time Limit: 15000/10000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit  Status ...

  7. Codeforces Round #681 (Div. 2, based on VK Cup 2019-2020 - Final) B. Saving the City (贪心,模拟)

    题意:给你一个\(01\)串,需要将所有的\(1\)给炸掉,每次炸都可以将一整个\(1\)的联通块炸掉,每炸一次消耗\(a\),可以将\(0\)转化为\(1\),消耗\(b\),问将所有\(1\)都炸 ...

  8. 牛客小白月赛28 D.位运算之谜 (位运算)

    题意:给你两个正整数\(x\)和\(y\),求两个正整数\(a\),\(b\),使得\(a+b=x\),\(a\)&\(b\)=\(y\),如果\(a\),\(b\),输出\(a\ xor \ ...

  9. Gym 102263 ArabellaCPC 2019 J - Thanos Power (DP,数学)

    题意:有一个整数\(n\),每次可以对加\(10^x\)或减\(10^x\),问最少操作多少次能得到\(n\). 题解:对于某一位上的数,我们可以从\(0\)加几次得到,或者从前一位减几次得到.所以对 ...

  10. Python3.7.9+Locust1.4.3版本性能测试工具案例分享

    一.Locust工具介绍 1.概述 Locust是一款易于使用的分布式负载测试工具,完全基于事件,使用python开发,即一个locust节点也可以在一个进程中支持数千并发用户,不使用回调,通过gev ...