2011-07-20 24 views
3

當另一個按鈕被點擊時,我試圖改變一個按鈕的backgroundWPF 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; 
     } 
    } 

感謝.....

+0

你的豬圈le定義背景=「透明」 – sll

+0

可能重複的[SyleProblem:不能更改特定按鈕的背景??????](http://stackoverflow.com/questions/6747709/syleproblem-cant-change-background-特別按鈕) – Rachel

回答

16

我已經在your other question

回答了這個自己的風格重新寫入按鈕的控件模板,所以背景顏色不再使用

默認按鈕模板看起來是這樣的:

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

而你overwritting模板說

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

更改您的ControlTemplate來

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

,它會工作

+0

@Aryan好的,但下次請留下評論,要求澄清,如果你不明白答案,或修改你的原始問題,使其更簡單,而不是重新發布相同的問題。 – Rachel

1

看起來這是在模板中定義的樣式(Background="Transparent")總是覆蓋您從代碼後面提供的值。嘗試從XAML中刪除它,並在構造函數中將其定義爲按鈕的默認背景。只是爲了檢查。 一般來說,我不會建議在後面的代碼中執行它,而是在XAML中移動此類邏輯。

1

在此行中:

<Border CornerRadius="2,2,2,2" HorizontalAlignment="Center" x:Name="borderTemplate" Background="Transparent"> 

您修復按鈕的透明背景。它不再鏈接到按鈕控件的Background屬性,因此更改該屬性不再傳播到樣式。

如果您使用的是「TemplatedParent」結合,將讓你重新連接控制,它的風格之間的聯繫 - 是這樣的:

<Border CornerRadius="2,2,2,2" HorizontalAlignment="Center" x:Name="borderTemplate" Background="{Binding RelativeSoure={RelativeSource TemplatedParent}, Path=Background}"> 

然後你可以設置按鈕的背景:

<Button Name="button1" Background="Transparent" ..... />