我有一个单选按钮,其样式已设置为使其显示为ToggleButton,并且我想将其默认背景颜色设置为绿色,但是当它处于活动状态/选中时,我希望它更改颜色。在下面的示例中,我希望它是蓝色的。无论我尝试什么,绿色都不会被覆盖。我想念什么?
按钮:
<RadioButton GroupName="Rating" Background="Green" Style="{StaticResource RatingToggleButtons}" x:Name="Normal" Content="Normal" Grid.Row="0" />
样式:
<Style x:Key="RatingToggleButtons" TargetType="{x:Type ToggleButton}" BasedOn="{StaticResource MahApps.Metro.Styles.MetroToggleButton}">
<Setter Property="Margin" Value="5 5 5 5" />
<Setter Property="MinWidth" Value="100"/>
<Setter Property="MinHeight" Value="50"/>
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Background" Value="Blue"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
答案 0 :(得分:0)
简而言之,与通过样式设置的背景(您的情况)相比,通过控制模板应用的背景占了上风。
<RadioButton.Template>
<ControlTemplate TargetType="{x:Type ButtonBase}">
<ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Background" TValue="#FF838383"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</RadioButton.Template>
注意:代码未经测试
答案 1 :(得分:0)
我想念什么?
局部值优先于样式设置的值:https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/dependency-property-value-precedence
因此,不必像这样设置本地值:
<RadioButton ... Background="Green" ... />
...您应该在Style
中定义默认值:
<Style TargetType="{x:Type ToggleButton}">
<Setter Property="Background" Value="Green"/>
<Setter Property="Margin" Value="5 5 5 5" />
<Setter Property="MinWidth" Value="100"/>
<Setter Property="MinHeight" Value="50"/>
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Background" Value="Blue"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
...并再次避免设置Background
元素的RadioButton
属性:
<RadioButton GroupName="Rating" Style="{StaticResource RatingToggleButtons}" x:Name="Normal" Content="Normal" Grid.Row="0" />