ContentControl 和 ContentPresenter 之间有什么区别? [英] What's the difference between ContentControl and ContentPresenter?

查看:18
本文介绍了ContentControl 和 ContentPresenter 之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定什么时候应该使用 ContentPresenter 而不是 ContentControl(反之亦然).目前,我几乎一直在我的 DataTemplate 中使用 ContentControl.ContentPresenter 什么时候会是更好的选择?为什么?

I'm not sure when I should use ContentPresenter instead of ContentControl (and vice-versa). At the moment, I'm using ContentControl pretty much all the time in my DataTemplates. When would ContentPresenter be a better choice? and why?

推荐答案

ContentControl 是包含其他元素并具有 Content 属性(用于例如,按钮).

ContentControl is a base class for controls that contain other elements and have a Content-property (for example, Button).

ContentPresenter 用于在控件模板中显示内容.

ContentPresenter is used inside control templates to display content.

ContentControl,当直接使用时(它应该用作基类),有一个使用 ContentPresenter 来显示其内容的控件模板.

ContentControl, when used directly (it's supposed to be used as a base class), has a control template that uses ContentPresenter to display it's content.

我的经验法则(并非适用于所有情况,请自行判断):

My rules of thumb (not applicable in every case, use your judgment):

  1. ControlTemplate内使用ContentPresenter
  2. ControlTemplate 之外(包括DataTemplate 和外部模板)尽量不要使用它们中的任何一个,如果需要,您必须首选ContentPresenter
  3. 子类 ContentControl 如果您正在创建一个承载内容的自定义无外观"控件,并且您无法通过更改现有控件的模板(这应该非常罕见)获得相同的结果.
  1. Inside ControlTemplate use ContentPresenter
  2. Outside of ControlTemplate (including DataTemplate and outside templates) try not to use any of them, if you need to, you must prefer ContentPresenter
  3. Subclass ContentControl if you are creating a custom "lookless" control that host content and you can't get the same result by changing an existing control's template (that should be extremely rare).

这篇关于ContentControl 和 ContentPresenter 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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