當另一個按鈕被點擊時,我試圖改變一個按鈕的background
。WPF STYLE:無法更改按鈕的背景?
我不能做到這一點,如果我提供風格的按鈕。
看到我下面的代碼。
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;
}
}
感謝.....
你的豬圈le定義背景=「透明」 – sll
可能重複的[SyleProblem:不能更改特定按鈕的背景??????](http://stackoverflow.com/questions/6747709/syleproblem-cant-change-background-特別按鈕) – Rachel