WPF中控件模板和数据模板之间的区别 [英] Difference between Control Template and DataTemplate in WPF

查看:533
本文介绍了WPF中控件模板和数据模板之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF中ControlTemplateDataTemplate有什么区别?

What is difference between a ControlTemplate and a DataTemplate in WPF?

推荐答案

通常情况下,控件是为自身目的而呈现的,并且不会反映基础数据.例如,Button不会绑定到业务对象-纯粹是在那里,因此可以单击它.但是,通常会出现ContentControlListBox,以便它们可以为用户显示数据.

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屋!

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