2017-06-15 62 views
0

我在頁面上有50個按鈕,所以我聲明瞭一個樣式,它將聲明適用於所有按鈕的邊框和IsPressed行爲。WPF Button ...有沒有一種方法來獨立於樣式聲明設置背景顏色

<UserControl.Resources> 
    <statusModule:BooleanToBackgroundColorConverter x:Key="BooleanToBackgroundColor"/> 
    <Style x:Key="ValveButtonStyle" TargetType="Button"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="Button"> 
        <Border Name="border" Background="Transparent" BorderThickness="1" BorderBrush="Black"> 
         <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> 
        </Border> 
        <ControlTemplate.Triggers> 
         <Trigger Property="Button.IsPressed" Value="True"> 
          <Setter TargetName="border" Property="BorderThickness" Value="3" /> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</UserControl.Resources> 

我的問題是,每個按鈕的背景顏色是通過一個單獨的視圖模型屬性控制,如

<Button Content="Deflection Gas Valve" Background="{Binding Path=OpenDeflectionGasValve, Converter={StaticResource BooleanToBackgroundColor}, Mode=OneWay}" Style="{StaticResource ValveButtonStyle}" 
<Button Content="Purge Gas Valve" Background="{Binding Path=OpenPurgeGasValve, Converter={StaticResource BooleanToBackgroundColor}, Mode=OneWay}" Style="{StaticResource ValveButtonStyle}" 

和另一個按鈕背景結合。將一個完全不同的視圖模型屬性。

如果我爲按鈕指定一個樣式,那麼上面聲明的背景設置不起作用。

有沒有一種方法可以使用所有按鈕的樣式,但是如上所示爲每個按鈕聲明一個背景值。

+0

粘貼,以幫助提到的風格! –

回答

1

在您的ConroleTemplate中,您需要尊重TextBox控件的原始Background屬性,通過TemplateBinding。 您的模板中未提及的每個TextBox屬性(屬性)都變得毫無意義。

所以更改

Background="Transparent" 

到:現在

Background="{TemplateBinding Background}" 

,背景將可以通過在background文本框屬性的值的影響。 和集默認值的背景屬性,通過設置器所設置的樣式顏色:

<Setter Property="Background" Value="Transparent" /> 
+0

完美!非常棒!非常感謝! – chuckp