.net ContentControl 和 ContentPresenter 之间有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1287995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:09:44  来源:igfitidea点击:

What's the difference between ContentControl and ContentPresenter?

.netwpfcontentcontrolcontentpresenter

提问by Wilka

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

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

采纳答案by Nir

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

ContentControl是包含其他元素并具有Content- 属性(例如,Button)的控件的基类。

ContentPresenteris used inside control templates to display content.

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.

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

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

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

  1. Inside ControlTemplateuse ContentPresenter
  2. Outside of ControlTemplate(including DataTemplateand outside templates) try not to use any of them, if you need to, you must prefer ContentPresenter
  3. Subclass ContentControlif 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).
  1. 内部ControlTemplate使用ContentPresenter
  2. 外部ControlTemplate(包括DataTemplate和外部模板)尽量不要使用它们中的任何一个,如果需要,您必须更喜欢ContentPresenter
  3. 子类ContentControl如果要创建一个自定义的“无外观的”控制主机的内容和你不能改变现有的控件模板相同的结果(这应该是极为罕见)。

回答by Thomas Levesque

ContentPresenter is usually used in a ControlTemplate, as a placeholder to say "put the actual content here".

ContentPresenter 通常用于 ControlTemplate 中,作为占位符表示“将实际内容放在这里”。

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

ContentControl 可以在任何地方使用,不一定在模板中。它将选取为分配给它的内容类型定义的任何 DataTemplate

回答by Charles HETIER

I recently wrote a post on my blog regarding these two controls:

我最近在我的博客上写了一篇关于这两个控件的文章:

ContentPresenter vs ContentControl(EDIT: Broken link replaced with archived version.)

ContentPresenter 与 ContentControl(编辑:损坏的链接替换为存档版本。)

The ContentPresenter.ContentSourceis 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 its ControlTemplate:

ContentPresenter.ContentSource就是实际上使得两个类之间的最大区别。ContentSource 属性仅在 ControlTemplate 中有意义;它确定内容应该映射到哪个 TemplatedParent 属性。例如,如果一个控件包含一个依赖属性MyProperty1,那么我们可能会在它的 中找到以下内容ControlTemplate

<ControlTemplate TargetType="MyControl" >
    [...]
       <ContentPresenter ContentSource="MyProperty1" />
    [...]
</ControlTemplate>

The content of the ContentPresenter will receive the value of MyProperty1.

ContentPresenter 的内容将收到 的值MyProperty1

Please note that if the name of the property is Content, there is no need to specify ContentSourceas it is the default value.

请注意,如果属性名称为Content,则无需指定,ContentSource因为它是默认值。

For those who know angularJs: this is similar to transclude mecanism.

对于那些了解 angularJs 的人:这类似于 transclude mecanism。

回答by Herman Van Der Blom

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:

这是一个老问题,但我刚刚完成了基于通用应用程序模板的动画平铺控件的开发,请查看旧电话 WP7/8 SDK 中的此代码:

<ContentControl x:Name="contentControl" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch" VerticalContentAlignment="Stretch">
    <ContentPresenter x:Name="contentPresenter" CacheMode="BitmapCache"/>
</ContentControl>

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 ControlTemplateanother container you can put an extra Container: ContentControlin it and for presenting the content a separate ContentPresenter. If you dont need a separate container then just use ControlTemplateand ControlPresentersfor 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 是用于显示内容的 Container 和 Presenter。在大多数情况下 ControlTemplate 将是容器,但如果您想在ControlTemplate另一个容器中,您可以放置​​一个额外的容器:ContentControl在其中并为呈现内容单独的ContentPresenter. 如果您不需要单独的容器,则只需使用ControlTemplateControlPresenters用于显示内容块至少那是微软的人在开发 WP7/8 SDK 时所做的。ContentControl 也可用于显示内容,但它同时用作容器和展示器。所以在上面的示例代码中,它的用途被拆分为 Container 和 Presenter。在动态示例中,您可以显示容器(它可以有一个空的背景或尚不存在的东西),然后用演示者内容动态填充它。容器具有尺寸(宽度、高度等),您将这些属性放在容器控件上并在其上显示内容。在示例中,ContentControl 确定必须对演示者内容执行哪些操作。

回答by Wayne Lo

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.

有时一个例子比理论术语更容易。在 MS 网站中(滚动到底部:http: //msdn.microsoft.com/en-us/library/system.windows.controls.contentpresenter(v= vs.110) .aspx),它使用一个按钮作为一个例子。Button 有一个 ContentControl,它允许您放置一个控件或一个自定义控件,这些控件可以是图像、文本、复选框、StackPanel、网格等。

After the customization of Button, now on the Xaml, you can write

Button自定义后,现在在Xaml上,可以写

<my:Button>
   <my:Button.Content>
      <my:AnotherControl>
   </my:Button.Content>
</my:Button>

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.

在上面的示例代码中,“my:Button.Content”是 ContentControl。AnotherControl 将放置在您指定的 ContentPresenter 所在的位置。

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.

同样,当比较 TextBox 和 TextBlock 时,TextBox 有一个 ContentPresenter 供您在其中填充内容,就像上面的 Button 示例一样,而 TextBlock 则没有。TextBlock 只允许您输入文本。