开发者

Whats the difference between ContentControl.Template and ContentControl.ContentTemplate

开发者 https://www.devze.com 2023-04-08 11:51 出处:网络
What\'s the difference between ContentControl.Template and ContentControl.ContentTemplate? And when do I use which?

What's the difference between ContentControl.Template and ContentControl.ContentTemplate? And when do I use which?

For example I could write in a xaml file for WPF:

<ContentControl>
    <ContentControl.Template>开发者_如何学Go
        <ControlTemplate>
            <Label Content="This is from the Template"/>
        </ControlTemplate>
    </ContentControl.Template>
</ContentControl>

Notice the ContentControl.Template Tag

or I could write this:

<ContentControl>
    <ContentControl.ContentTemplate>
        <DataTemplate>
            <Label Content="This is From the ContentTemplate"/>
        </DataTemplate>
    </ContentControl.ContentTemplate>
</ContentControl>

Notice the ContentControl.ContentTemplate Tag

The output looks the same and in the first case I use a ControlTemplate and in the other a DataTemplate. However, how should I deceide if I have to use .Template or .ContentTemplate? And what implications does this have (e. g. on DataBinding, Property Inheritance, ...).


Template property defines the appearence of a Control itself and ContentTemplate defines the template of the Content area of a Control. Interesting point from MSDN:

If a Control does not have a ControlTemplate, the Control will not appear in your application.

This becomes more clear when we take a look at the data types of both properties:

  • Template property type is ControlTemplate
  • ContentTemplate property type is DataTemplate and you can switch it in runtime using ContentControl.ContentTemplateSelector
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号