ItemTemplate 与 ControlTemplate [英] ItemTemplate vs ControlTemplate

查看:26
本文介绍了ItemTemplate 与 ControlTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对学习 WPF 感到困惑.我看到 ControlTemplate 用于确定列表框中每个项目的外观.这不是 ItemTemplate 在列表框中的用途吗?有什么区别?

I am confused with learning about WPF. I see ControlTemplate used to determine how each item in a listbox looks. Isn't that what the ItemTemplate is used for in listboxes? What is the difference?

推荐答案

A ControlTemplate 让你改变现有控件的外观和感觉,例如,使普通的日常按钮看起来像 Vista 的任务栏闪亮的玻璃按钮或制作选项卡控件看起来像新的 Office 2007 功能区.

A ControlTemplate let's you change the look&feel of existing controls, for example, make a normal everyday button look like Vista's taskbar shiny glass buttons or make a tab control look like the new Office 2007 ribbon.

这个想法是,控件只包含行为,而 ControlTemplate 包含外观和感觉——所以按钮是你点击的东西"而不是你可以点击的带有一些 3d 效果的矩形".

The idea is that the control contains only the behavior and the ControlTemplate contains the look&feel - so a button is "something you click" not "a rectangle with some 3d effect you can click on".

您可以替换 ListBox 的控件模板以获得类似于带有圆角或自定义滚动条的列表框的内容.

You can replace the ListBox's control template to get something like a list box with rounded corners or custom scroll bars.

A DataTemplate(ItemTemplate 是 DataTemplate 类型的属性)另一方面让您控制数据行在列表框(和其他类似控件)内的显示方式,数据模板用于指定诸如显示姓氏,而不是逗号和名字 - 如果姓氏以 A" 开头,则姓氏应为粗体,名字应为绿色.

A DataTemplate (ItemTemplate is a property of type DataTemplate) on the other hand let's you control how the data rows displayed inside list boxes (and other similar controls), data templates are used to specify things like "Show last name, than a comma and the first name - and the last name should be bold and the first name should be green if it starts with an A".

为了完整起见,还有一个 ItemsPanelTemplate 类用于更改列表框的内部布局 - Charles Petzold 的 WPF 书中有一个示例,使用它来使普通列表框显示其项目在一个圆圈中.

And just for completeness there is also an ItemsPanelTemplate class that is used to change the internal layout of a list box - Charles Petzold's WPF book has an example of using it to make a normal list box display it's items in a circle.

这篇关于ItemTemplate 与 ControlTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆