ContentControl 和 ContentPresenter 有什么区别?

发布于 2024-08-02 00:59:50 字数 206 浏览 5 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

仄言 2024-08-09 00:59:50

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

ContentPresenter 在控件模板内使用来显示内容。

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

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

  1. ControlTemplate 内部使用 ContentPresenter
  2. ControlTemplate 外部(包括 DataTemplate 和外部模板)尽量不要使用它们中的任何一个,如果需要的话,
  3. 如果您要创建自定义“lookless”,则必须更喜欢 ContentPresenter 子类 ContentControl控制托管内容,并且您无法通过更改现有控件的模板来获得相同的结果(这种情况应该极其罕见)。

ContentControl is a base class for controls that contain other elements and have a Content-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):

  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).
江挽川 2024-08-09 00:59:50

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

同展鸳鸯锦 2024-08-09 00:59:50

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

ContentPresenter 与 ContentControl

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

<ControlTemplate TargetType="MyControl" >
    [...]
       <ContentPresenter ContentSource="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 its ControlTemplate:

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

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 specify ContentSource as it is the default value.

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

寄人书 2024-08-09 00:59:50

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

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

在这里您可以看到 ContentControl 是用于显示内容的容器和演示者。 在大多数情况下,ControlTemplate 将是容器,但如果您想在 ControlTemplate 中放置另一个容器,您可以在其中放置一个额外的容器:ContentControl,并用于呈现内容一个单独的 <代码>ContentPresenter。 如果您不需要单独的容器,那么只需使用 ControlTemplateControlPresenters 来显示内容块,至少 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:

<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 ControlTemplate another container you can put an extra Container: ContentControl in it and for presenting the content a separate ContentPresenter. If you dont need a separate container then just use ControlTemplate and ControlPresenters 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.

旧话新听 2024-08-09 00:59:50

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

西瓜 2024-08-09 00:59:50

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

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

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

在上面的示例代码中,“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

<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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文