惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
Scott Helme
Scott Helme
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Stack Overflow Blog
Stack Overflow Blog
Forbes - Security
Forbes - Security
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
S
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Cisco Talos Blog
Cisco Talos Blog
N
News | PayPal Newsroom
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Project Zero
Project Zero
Cyberwarzone
Cyberwarzone
Vercel News
Vercel News
M
MIT News - Artificial intelligence
云风的 BLOG
云风的 BLOG
Google Online Security Blog
Google Online Security Blog
Simon Willison's Weblog
Simon Willison's Weblog
MongoDB | Blog
MongoDB | Blog
Martin Fowler
Martin Fowler
T
Tenable Blog
I
InfoQ
W
WeLiveSecurity
Google DeepMind News
Google DeepMind News
G
Google Developers Blog
D
DataBreaches.Net
博客园 - Franky
Know Your Adversary
Know Your Adversary
Spread Privacy
Spread Privacy
S
Security @ Cisco Blogs
Hacker News: Ask HN
Hacker News: Ask HN
J
Java Code Geeks
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
F
Full Disclosure
C
Cisco Blogs
Google DeepMind News
Google DeepMind News
P
Proofpoint News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Cloudflare Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
I
Intezer
The GitHub Blog
The GitHub Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
F
Fortinet All Blogs
Jina AI
Jina AI
V
Visual Studio Blog
L
LINUX DO - 热门话题
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏

博客园 - MainTao

WebStorm 自定义字体+颜色+语法高亮+导入导出用户设置 WebStorm安装Vim以及快捷键设置 scroll clock–jQuery widget implement 基于iScroll 的开发(不断补充) C#操作Win7/Win8的库、收藏夹 P/Invoke Interop 实例 WP7图片从独立存储绑定 编写jQueryUI插件(widget) 调试Javascript Windows Phone 7监测网络环境变化 Blend制作动画 用OpacityMask快速制作theme friendly UI 用gradient brush和OpacityMask实现fade edge效果 blend 画图 PhoneApplicationFrame以及设置Obscured/Unobscured的event handler Windows Phone 7 短信表情 WP7应用程序生命周期,以及各个event handler分别该做什么 第一个WP7程序 开发总结 XAML中的空格、换行、Tab
ControlTemplate & DataTemplate
MainTao · 2011-12-08 · via 博客园 - MainTao

ControlTemplate和DataTemplate都是用于定制控件的外观,但两者有着很大的区别。

ControlTemplate服务于Control(有形的控件),而DataTemplate服务于Data(无形的数据)。

换句话说,ControlTemplate用于将已有的外观进行改造,DataTemplate是从无到有地构建外观。

ContentControl、ItemsControl类型

  • ContentControl,它的Content属性包含单个元素,例如Button
  • ItemsControl,它的Items属性包含多个元素,例如Listbox

ContentPresenter、ItemsPresenter类型

平常的使用中,可以认为这些Presenter是占位符,用于ControlTemplate的定义中,代表了控件原本的Content。

以ContentControl为例,它有一个ContentPresenter类型的属性,如果其值是一个UIElement,那就直接显示这个UIElement,但如果是某个无形的数据,就要让ContentPresenter的值是一个DataTemplate。

ControlTemplate类型

ControlTemplate的代表:

  • ContentControl类型的Template属性
  • ItemsControl类型的Template属性

ControlTemplate类型中常见到{TemplateBinding 属性名},一般表示与原有控件的属性绑定。

DataTemplate类型

DataTemplate的典型代表:

  • ContentControl类型的ContentTemplate属性
  • ItemsControl类型的ItemTemplate属性

DataTemplate类型中常见到{Binding …},表示数据绑定。

以下不属于Template,但是经常会与Template一起出现,所以写在这里。

ItemsPanel类型

表示ItemsControl各个Item是如何布局的,常用的也就是StackPanel横过来竖过来这些。

ItemContainerStyle类型

定制ItemsControl中各个Item的Style。如果要定制ItemsControl总框架的Style,就直接用ItemsControl的Style属性。

我的体会

DataTemplate比ControlTemplate更常用。

了解一个控件的Template(ControlTemplate类型),需要用blend查看控件的VisualTree,查起来改起来都比较麻烦。但有些情况下用ControlTemplate很有效率,例如把所有的TextBox都换成圆角的、给button改个形状加个图片。对于伤筋动骨的改造,需要ControlTemplate,而对于一般的描眉画眼(给现有的属性赋新值),只需要在Style的Setter里给要改变的属性赋值就可以了。