2016-06-28 40 views
1

我有这些控件:XAML局部造型?

<Border Style="{StaticResource Button}">     
    <TextBlock>NEW</TextBlock> 
</Border> 
<Border Style="{StaticResource Button}"> 
    <TextBlock>CLOSE</TextBlock> 
</Border> 
<Border Style="{StaticResource Button}"> 
    <TextBlock>EXIT</TextBlock> 
</Border> 

而且这一款式:

<Style x:Key="Button" TargetType="{x:Type Border}"> 
    <Setter Property="Padding" Value="15, 10" /> 
    <Setter Property="Margin" Value="5, 0" /> 
    <Setter Property="MinWidth" Value="150" /> 
    <Style.Triggers> 
     <Trigger Property="IsMouseOver" Value="True"> 
      <Setter Property="Cursor" Value="Hand" />     
     </Trigger> 
    </Style.Triggers> 
    <Style.Resources> 
     <Style TargetType="{x:Type TextBlock}"> 
      <Setter Property="VerticalAlignment" Value="Center" /> 
     </Style> 
    </Style.Resources>   
</Style> 

我想每个按钮/边框设置为特定的,不同颜色的每个,以及另一种颜色IsMouseOver。

我试着在控件上设置Background,但是样式不能覆盖背景,我不能做IsMouseOver的更改。 我只能想到用它们的颜色为每个人创建不同的风格,但无论如何要做一个部分样式,就像你在CSS中做的一样?

<div class="button blue">NEW</div> 
<div class="button red">CLOSE</div> 
<div class="button gray">EXIT</div> 

或者其他任何方式来实现这一目标?

回答

2

我有一段时间没有做过任何XAML,所以我有点生锈。我敢肯定,你不能做部分样式,但我知道你可以使用“BasedOn”继承样式。

https://msdn.microsoft.com/en-us/library/system.windows.style.basedon(v=vs.110).aspx

这将允许你做你的基础按钮样式,然后创建你需要基于共同的风格特点各颜色的颜色变异。

<Style x:Key="Button" TargetType="{x:Type Border}"> 
    <Setter Property="Padding" Value="15, 10" /> 
    <Setter Property="Margin" Value="5, 0" /> 
    <Setter Property="MinWidth" Value="150" /> 
    <Style.Triggers> 
     <Trigger Property="IsMouseOver" Value="True"> 
      <Setter Property="Cursor" Value="Hand" />     
     </Trigger> 
    </Style.Triggers> 
    <Style.Resources> 
     <Style TargetType="{x:Type TextBlock}"> 
      <Setter Property="VerticalAlignment" Value="Center" /> 
     </Style> 
    </Style.Resources>   
</Style> 

<Style x:Key="ButtonBlue" TargetType="{x:Type Border}" BasedOn="{StaticResource Button}"> 
    <Setter Property="Background" Value="Blue" /> 
</Style> 

类似的东西,然后你用“ButtonBlue”而不是“按钮”,只要您的XAML引用了要为蓝色控件的静态资源。您应该能够在继承样式中添加触发器。希望我能正确记住我的语法。

此外,您可能希望将样式键更改为“Button”以外的其他值。可能有点混乱。