本文介绍了ControlTemplate导致以下错误“属性'Content'设置不止一次".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是第一次尝试对要创建的按钮使用ControlTemplate.

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

但是,一旦我将标签<ControlTemplate>放在任何地方,就会出现错误.

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?

推荐答案

模板(如样式,笔刷,数据模板)是资源,通常放置在控件中的资源字典或资源部分中.

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>

这篇关于ControlTemplate导致以下错误“属性'Content'设置不止一次".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 15:14