控制模板:“内容设置不止一次”

时间:2014-10-23 17:35:27

标签: c# wpf templates

我正在尝试为按钮创建控制模板。我可以添加颜色和文本但不能编辑按钮的转换,因为这个错误: 'System.Windows.Controls.Button.Content' property has already been set and can be set only once. 但我设置了一次内容:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="Button" x:Name="redTitle" x:Key="redTitle">
        <!--Set to true to not get any properties from the themes.-->
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">

                    <Button Content="{TemplateBinding Content}" BorderBrush="Red" Foreground="White">
                        <VisualStateGroup Name="States">
                            <VisualStateGroup.Transitions>
                                <VisualTransition To="MouseOver" GeneratedDuration="0:0:0.01"></VisualTransition>
                            </VisualStateGroup.Transitions>
                        </VisualStateGroup>
                        <Button.Background>
                            <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                <GradientStop Color="Red"/>
                                <GradientStop Color="Red" Offset="1"/>
                            </LinearGradientBrush>
                        </Button.Background>
                    </Button>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="OverridesDefaultStyle" Value="True"/>
    </Style>

</ResourceDictionary>

错误在哪里?

1 个答案:

答案 0 :(得分:2)

您需要将<VisualStateGroup>元素包装在<VisualStateManager.VisualStateGroups>中。该错误告诉您,您已使用Content属性分配了按钮的内容,并隐式地将<VisualStateGroup>作为按钮的直接子项。您打算将后者放在属性设置器中。