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
Looks like a style which is defined in template (Background="Transparent"
) always overrides a value you've provided from code behind. Try to remove it from XAML
and define in constructor as default background of a button. Just to check.
In general I would not suggest to do it in code behind, move such logic in XAML
instead.
On this line:
<Border CornerRadius="2,2,2,2" HorizontalAlignment="Center" x:Name="borderTemplate" Background="Transparent">
You fix the Background of the button to transparent. It is no longer linked to the Background property of the button control so changing that property no longer propagates down to the style.
If you use a "TemplatedParent" binding it will allow you to reattach the link between the control and it's style - like this:
<Border CornerRadius="2,2,2,2" HorizontalAlignment="Center" x:Name="borderTemplate" Background="{Binding RelativeSoure={RelativeSource TemplatedParent}, Path=Background}">
And then you can set the background of the button:
<Button Name="button1" Background="Transparent" ..... />
精彩评论