The ControlTemplate is causing the following error “The property 'Content' is set more than once”

好久不见. 提交于 2020-01-16 03:53:08

问题


I'm just trying for the first time to use a ControlTemplate for a button that I want to create.

However, as soon as I put the tag <ControlTemplate> anywhere, it is coming up with an error.

<Window x:Class="MAQButtonTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="695" Width="996">        
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="300" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid Grid.Column="0" Background="#FFE9F1F6"></Grid>
        <Grid Grid.Column="1" Background="#FFD2E3ED">

        </Grid>
    </Grid>
    <ControlTemplate></ControlTemplate>
</Window>

Where do I put the tag so that this error doesn't come up?


回答1:


Templates, like Styles, Brushes, DataTemplates are Resources, and usually placed inside a resource dictionary or a resource section in your control.

<Window>
    <Window.Resources>
        <ControlTemplate TargetType="{x:Type Button}"/>
        <ControlTemplate x:Key="myTemplate" TargetType="{x:Type Button}"/>
    <Window.Resources>

    <!-- this will use your implicit defined template -->
    <Button />
    <!-- this will use your explicit defined template called  myTemplate-->
    <Button Template="{StaticResource myTemplate}"/>
</Window>


来源:https://stackoverflow.com/questions/10706043/the-controltemplate-is-causing-the-following-error-the-property-content-is-se

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!