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.....
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With