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 DataTemplate
s. When would ContentPresenter
be a better choice? and why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
ContentControl
是包含其他元素并具有Content
属性(例如Button
)的控件的基类。ContentPresenter
在控件模板内使用来显示内容。ContentControl
,当直接使用时(它应该用作基类),有一个使用 ContentPresenter 来显示其内容的控件模板。我的经验法则(并非适用于所有情况,请自行判断):
ControlTemplate
内部使用ContentPresenter
ControlTemplate
外部(包括DataTemplate
和外部模板)尽量不要使用它们中的任何一个,如果需要的话,ContentPresenter
子类ContentControl
控制托管内容,并且您无法通过更改现有控件的模板来获得相同的结果(这种情况应该极其罕见)。ContentControl
is a base class for controls that contain other elements and have aContent
-property (for example,Button
).ContentPresenter
is used inside control templates to display content.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):
ControlTemplate
useContentPresenter
ControlTemplate
(includingDataTemplate
and outside templates) try not to use any of them, if you need to, you must preferContentPresenter
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).ContentPresenter 通常在 ControlTemplate 中使用,作为占位符来表示“将实际内容放在这里”。
ContentControl 可以在任何地方使用,不一定在模板中。 它将选取为分配给它的内容类型定义的任何 DataTemplate
ContentPresenter is usually used in a ControlTemplate, as a placeholder to say "put the actual content here".
A ContentControl can be used anywhere, not necessarily in a template. It will pick up any DataTemplate defined for the type of content assigned to it
我最近在博客上写了一篇关于这两个控件的文章:
ContentPresenter 与 ContentControl
ContentPresenter.ContentSource 实际上是这两个类之间最大的区别。
ContentSource 属性仅在 ControlTemplate 中才有意义; 它确定内容应映射到哪个 TemplatedParent 属性。
例如,如果控件包含依赖属性
MyProperty1
,那么我们可能会在其ControlTemplate
中找到以下内容:ContentPresenter 的内容将接收
MyProperty1 的值
。请注意,如果属性名称为
Content
,则无需指定ContentSource
,因为它是默认值。对于那些了解 AngularJs 的人来说:这类似于嵌入机制。
I recently wrote a post on my blog regarding these two controls:
ContentPresenter vs ContentControl
The ContentPresenter.ContentSource is what actually makes the biggest difference between the two classes.
ContentSource property makes sense only within a ControlTemplate; it determines which TemplatedParent property the content should be mapped with.
For example, if a control contains a dependency property
MyProperty1
, then we might find the following within itsControlTemplate
:The content of the ContentPresenter will receive the value of
MyProperty1
.Please note that if the name of the property is
Content
, there is no need to specifyContentSource
as it is the default value.For those who know angularJs: this is similar to transclude mecanism.
这是一个老问题,但我刚刚完成动画平铺控件的开发,基于通用应用程序的模板,请查看旧手机 WP7/8 SDK 中的代码:
在这里您可以看到 ContentControl 是用于显示内容的容器和演示者。 在大多数情况下,ControlTemplate 将是容器,但如果您想在
ControlTemplate
中放置另一个容器,您可以在其中放置一个额外的容器:ContentControl
,并用于呈现内容一个单独的 <代码>ContentPresenter。 如果您不需要单独的容器,那么只需使用ControlTemplate
和ControlPresenters
来显示内容块,至少 Microsoft 的人员在开发 WP7/8 SDK 时就是这样做的。 ContentControl 也可用于显示内容,但它同时充当容器和演示者。 因此,在上面的示例代码中,其用途分为 Container 和 Presenter。 在动态示例中,您可以显示容器(它可以具有空背景或尚不存在的背景),然后用演示者内容动态填充它。 容器具有尺寸(宽度、高度等),您将这些属性放在容器控件上并在其上显示内容。 在示例中,ContentControl 确定必须对演示者内容执行哪些操作。Its an old question but I was just finishing developing an animated Tile Control, template based for a universal app, look at this code from the old Phone WP7/8 SDK:
Here you can see the ContentControl is the Container and the Presenter for displaying content. In most cases the ControlTemplate will be the Container but if you want in your
ControlTemplate
another container you can put an extra Container:ContentControl
in it and for presenting the content a separateContentPresenter
. If you dont need a separate container then just useControlTemplate
andControlPresenters
for displaying content blocks at least thats what the guys at Microsoft did when they developed the WP7/8 SDK. The ContentControl can also be used for displaying content but then it serves both as container and presenter. So in the sample code above its purpose is splitted in Container and Presenter. In dynamic samples you could display the container (it can have an empty background or something thats not there yet) and then dynamically fill it with the presenter content. A container has dimensions (width,height etc.), you put those properties on the container control and present content on it. In the sample the ContentControl determines what has to be done with the presenter content.ContentControl
是制作WPF自定义控件(不要与用户控件混淆)。 所以这是你的顶级课程。ContentPresenter
就像任何其他“常规”控件一样。 您可以在自定义或用户控件中或在模板中或仅在标记中使用它ContentControl
is to make a WPF custom control (dont mix up with user control). So it is your top-level class.ContentPresenter
is just like any other "regular" control. You can use it in custom or user control or in a template or just in markup有时例子比理论术语更容易。 在 MS 网站中(滚动到底部:http://msdn.microsoft.com/en-us/library/system.windows.controls.contentpresenter(v=vs.110).aspx),它使用按钮作为示例。 Button 有一个 ContentControl,它允许您放置一个控件或自定义控件,可以是图像、文本、复选框、StackPanel、网格等。
自定义Button之后,现在在Xaml上,可以这样写
在上面的示例代码中,“my:Button.Content”就是ContentControl。 AnotherControl 将放置在您指定的 ContentPresenter 所在位置。
同样,当比较 TextBox 和 TextBlock 时,TextBox 有一个 ContentPresenter 供您在其中填充内容,就像上面的 Button 示例一样,而 TextBlock 则没有。 TextBlock 只允许您输入文本。
Sometimes an example is easier than theoretical jargon. In an MS web site (Scroll to the bottom: http://msdn.microsoft.com/en-us/library/system.windows.controls.contentpresenter(v=vs.110).aspx), it uses a button as an example. A Button has a ContentControl, which allows you to place one control or a custom control that could be an Image, Text, CheckBox, StackPanel, Grid, whatever.
After the customization of Button, now on the Xaml, you can write
In the above example code, the "my:Button.Content" is the ContentControl. The AnotherControl will be place to what you had specified where the ContentPresenter is.
Similarly, when compares TextBox and TextBlock, TextBox has a ContentPresenter for you to stuff stuff in it just like the above Button example whereas a TextBlock doesn't. A TextBlock only allows you to enter text.