WPF中控件模板和数据模板之间的区别 [英] Difference between Control Template and DataTemplate in WPF
问题描述
在WPF中ControlTemplate
和DataTemplate
有什么区别?
What is difference between a ControlTemplate
and a DataTemplate
in WPF?
推荐答案
通常情况下,控件是为自身目的而呈现的,并且不会反映基础数据.例如,Button
不会绑定到业务对象-纯粹是在那里,因此可以单击它.但是,通常会出现ContentControl
或ListBox
,以便它们可以为用户显示数据.
Typically a control is rendered for its own sake, and doesn't reflect underlying data. For example, a Button
wouldn't be bound to a business object - it's there purely so it can be clicked on. A ContentControl
or ListBox
, however, generally appear so that they can present data for the user.
因此,DataTemplate
用于为基础数据提供视觉结构,而ControlTemplate
与基础数据无关,而只是为控件本身提供视觉布局.
A DataTemplate
, therefore, is used to provide visual structure for underlying data, while a ControlTemplate
has nothing to do with underlying data and simply provides visual layout for the control itself.
ControlTemplate
通常只包含TemplateBinding
表达式,绑定回控件本身的属性,而DataTemplate
将包含标准的Binding表达式,绑定到其DataContext
的属性(业务/域对象或视图模型).
A ControlTemplate
will generally only contain TemplateBinding
expressions, binding back to the properties on the control itself, while a DataTemplate
will contain standard Binding expressions, binding to the properties of its DataContext
(the business/domain object or view model).
这篇关于WPF中控件模板和数据模板之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!