【问题标题】:Button Background property not working按钮背景属性不起作用
【发布时间】:2014-11-09 06:13:06
【问题描述】:

我的代码中有一个带有样式的按钮。样式在 .xaml 文件的资源中:

<Style x:Key="RoundCorner" TargetType="{x:Type Button}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Border CornerRadius="8" BorderBrush="#006AB6" BorderThickness="1" Name="border" >
                                <Grid x:Name="grid" >
                                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" ></ContentPresenter>
                                </Grid>
                            </Border>
                            <ControlTemplate.Triggers></ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

这是按钮的代码:

<Button Name="btnZ" Background="Red" Content="{Binding Z}" Grid.Column="2" Style="{DynamicResource RoundCorner}" Visibility="{Binding Path=IsButtonVisible, Converter={StaticResource boolToVisibilityConverter}}"/>

按钮的背景属性(我将属性设置为红色)不起作用。为什么会这样?

【问题讨论】:

    标签: wpf xaml button background


    【解决方案1】:

    您必须使用 TemplateBinding 将 Button 的 Background 映射到内部 ControlTemplate 的 Background(其根视觉对象是 Border)(为方便起见):

    <ControlTemplate TargetType="{x:Type Button}">
      <Border CornerRadius="8" BorderBrush="#006AB6" BorderThickness="1" 
              Name="border"
              Background="{TemplateBinding Background}"
       />
       <!-- ... -->
    </ControlTemplate>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-19
      • 1970-01-01
      • 1970-01-01
      • 2016-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多