Wpf 按钮背景属性不工作

Wpf 按钮背景属性不工作,wpf,xaml,button,background,Wpf,Xaml,Button,Background,我的代码中有一个带有样式的按钮。样式位于.xaml文件的资源中: <Style x:Key="RoundCorner" TargetType="{x:Type Button}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Typ

我的代码中有一个带有样式的按钮。样式位于.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}}"/>


按钮的背景属性(我在其中将属性设置为红色)不起作用。为什么会发生这种情况?

您必须使用
模板绑定将按钮的
背景
映射到内部控件模板(其根视觉为
边框
)的
背景
(为方便起见):


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