2014-06-10 97 views
2

我有一個風格的按鈕,但我有幾個,並希望風格只適用於一個按鈕。我該怎麼做?只適用於WPF風格的一個按鈕

<Style TargetType="{x:Type Button}"> 
    <Setter Property="Background" > 
     <Setter.Value> 
      <ImageBrush ImageSource="resources/main_play_normal.png"/> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Button}"> 
       <Border Background="{TemplateBinding Background}"> 
        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Style.Triggers> 
     <Trigger Property="IsMouseOver" Value="True"> 
      <Setter Property="Background" > 
       <Setter.Value> 
        <ImageBrush ImageSource="resources/main_play_over.png"/> 
       </Setter.Value> 
      </Setter> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

回答

7

給它一個關鍵:

<Style TargetType="{x:Type Button}" x:Key="AButtonStyle"> 
</Style> 

然後將它應用到按鈕:

<Button Style="{StaticResource AButtonStyle}" ... /> 

您還可以通過使用正常樣式應用於任何控制用於設置屬性的XAML語法:

<Button> 
    <Button.Style> 
     <Style TargetType="{x:Type Button}"> 
     </Style> 
    </Button.Style> 
</Button>