将ControlTemplate值绑定到样式属性

本文关键字:样式 属性 绑定 ControlTemplate | 更新日期: 2023-09-27 17:57:43

我的自定义按钮有一个ControlTemplate

我正在尝试将ControlTemplate中的<Border.Background>绑定到Styles Background属性。

XAML

<ControlTemplate x:Key="NumberButtonControlTemplate" TargetType="Button" >
    <Border x:Name="Border">
        <Border.Background>
            <SolidColorBrush Color="{TemplateBinding Background}" />
        </Border.Background>
    </Border>
</ControlTemplate>

<Style x:Key="NumberButtonStyle" TargetType="Button">
    <Setter Property="Template" Value="{StaticResource NumberButtonControlTemplate}" />        
    <Setter Property="Background" Value="MediumSpringGreen" />
    <Setter Property="Height" Value="80" />
</Style>

如何使ControlTemplate BorderBackground属性获得样式背景的值?

我上面所做的是对的,是不是我遗漏了什么?

将ControlTemplate值绑定到样式属性

Button(和Border)上的Background属性的类型为Brush,但您正试图将其用作Color。使用这个替代:

<Border x:Name="Border" Background="{TemplateBinding Background}"/>