简体   繁体   English

将ControlTemplate值绑定到样式属性

[英]Bind a ControlTemplate value to a styles property

I have a ControlTemplate for my custom button. 我的自定义按钮有一个ControlTemplate

Im trying to bind the <Border.Background> in the ControlTemplate to the Styles Background property. 我试图将ControlTemplate中的<Border.Background>绑定到Styles Background属性。

XAML 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>

How do I make the ControlTemplate BorderBackground Property get the value of the Style Background ? 如何使ControlTemplate BorderBackground属性获得样式背景的值?

What I have done above is right, is there something Im missing ? 我上面所做的是正确的,我缺少什么吗?

The Background property on Button (and on Border ) is of type Brush but you're attempting to use it as a Color . Button (和Border )上的Background属性的类型为Brush但是您尝试将其用作Color Use this instead: 使用此代替:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM