【问题标题】:Bind a ControlTemplate value to a styles property将 ControlTemplate 值绑定到样式属性
【发布时间】:2014-05-23 10:02:21
【问题描述】:

我的自定义按钮有一个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 属性获取样式背景的值?

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

【问题讨论】:

    标签: c# .net silverlight xaml styles


    【解决方案1】:

    Button(和Border)上的Background 属性属于Brush 类型,但您正尝试将其用作Color。改用这个:

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

    【讨论】:

    • 我想我不小心在笔记本电脑上投了反对票。 SO声明如果你编辑你的分析器,我可以改变我的投票(对不起,我不知道我投了反对票)
    猜你喜欢
    • 2015-10-07
    • 2020-02-11
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多