【发布时间】:2017-11-18 13:43:26
【问题描述】:
我在一个页面上有 50 个按钮,因此我声明了一个样式,该样式将声明适用于所有按钮的边框和 IsPressed 行为。
<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>
我遇到的问题是每个按钮的背景颜色都是通过单独的 ViewModel 属性控制的,例如
<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}"
另一个按钮背景绑定将是一个完全不同的 ViewModel 属性。
如果我为按钮分配样式,那么上面声明的背景设置无效。
有没有一种方法可以为所有按钮使用样式,但为每个按钮声明一个背景值,如上所示。
【问题讨论】:
-
粘贴上面提到的样式以便帮助!