Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF STYLE: Can't change background of button?

I trying to change background of one button when another button clicked.

i can't do this if i providing style to button.

See my below code.

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>

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;
        }
    }

Thanks.....

like image 355
Aryan SuryaWansi Avatar asked Dec 02 '22 02:12

Aryan SuryaWansi


1 Answers

I already answered this in your other question

Your style re-writes the Button's ControlTemplate, so the Background color is no longer used

The Default button template looks like this:

<Button Background="SomeColor">
    <Button.Content>
</Button>

And you are overwritting the template to say

<Border>
    <Button.Content>
</Border>

Change your ControlTemplate to

<Border Background="{TemplateBinding Background}">
    <Button.Content>
</Border>

and it will work

like image 96
Rachel Avatar answered Dec 04 '22 22:12

Rachel