WPF STYLE:无法更改按钮的背景?

Ary*_*nsi 3 c# wpf xaml styles button

background当点击另一个按钮时,我尝试更改一个按钮.

如果我提供按钮样式,我不能这样做.

请参阅下面的代码.

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="Button" x:Key="TransparentButton">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border  CornerRadius="2,2,2,2"  HorizontalAlignment="Center" x:Name="borderTemplate" Background="Transparent">
                            <ContentPresenter/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter TargetName="borderTemplate"  Property="Border.BorderBrush" Value="Gray" />
                                <Setter TargetName="borderTemplate"  Property="Border.BorderThickness" Value="1" />
                            </Trigger>
                            <Trigger Property="IsPressed" Value="true">
                                <Setter TargetName="borderTemplate"  Property="Border.BorderBrush" Value="Lime" />
                            </Trigger>
                            <Trigger Property="IsFocused" Value="true">
                                <Setter TargetName="borderTemplate"  Property="Border.Background" Value="#FD7" />
                            </Trigger>

                            <Trigger Property="IsEnabled" Value="false">
                                <Setter TargetName="borderTemplate"  Property="Border.Background" Value="LightGray"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <Button Content="Button" Style="{StaticResource TransparentButton}" Height="23" HorizontalAlignment="Left" Margin="20,25,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="143,177,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

MainWindow.xaml.cs

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            //This doesn't work if i providing style to button  ==> Style="{StaticResource TransparentButton}"
            button1.Background = Brushes.Red;
        }
    }
Run Code Online (Sandbox Code Playgroud)

谢谢.....

Rac*_*hel 16

我已经在你的另一个问题中回答过了

您的样式重写了Button的ControlTemplate,因此不再使用Background颜色

默认按钮模板如下所示:

<Button Background="SomeColor">
    <Button.Content>
</Button>
Run Code Online (Sandbox Code Playgroud)

你正在覆盖模板说

<Border>
    <Button.Content>
</Border>
Run Code Online (Sandbox Code Playgroud)

将ControlTemplate更改为

<Border Background="{TemplateBinding Background}">
    <Button.Content>
</Border>
Run Code Online (Sandbox Code Playgroud)

它会起作用