为什么我在WPF中的触发事件不起作用



我是WPF的新手,我正试图用单选按钮创建一个触发事件,每当我选中一个单选按钮时,就会发生一些事情,比如更改背景或其他事情。这是我所拥有的:

<Border Width="157" Height="35" Margin="0 30 0 0" Background="#181F26" CornerRadius="3">
<Viewbox Height="29">
<RadioButton  
Click="rb_storno_Click"
x:Name="rb_storno"
Content="Storno"
FontSize="25"
Foreground="#BED9FF" 
FontFamily="Century Gothic" 
Background="#2683FF"
BorderThickness="2"
BorderBrush="#1D5BAC"                            
VerticalContentAlignment="Center" 
Margin="5,2,5,2">
<RadioButton.Style>
<Style TargetType="RadioButton">
<Style.Triggers>
<Trigger Property="IsChecked" Value="true">
<Setter Property="Foreground" Value="red"/>
</Trigger>
</Style.Triggers>
</Style>
</RadioButton.Style>

</RadioButton>
</Viewbox>

</Border>

但它不起作用,有什么建议可以让我做这件事吗?

您必须通过setter:设置初始前景值

<RadioButton
Click="rb_storno_Click"
x:Name="rb_storno"
Content="Storno"
FontSize="25"
FontFamily="Century Gothic" 
Background="#2683FF"
BorderThickness="2"
BorderBrush="#1D5BAC"
VerticalContentAlignment="Center" 
Margin="5,2,5,2">
<RadioButton.Style>
<Style TargetType="RadioButton">
<Setter Property="Foreground" Value="#BED9FF"/>
<Style.Triggers>
<Trigger Property="IsChecked" Value="true">
<Setter Property="Foreground" Value="red"/>
</Trigger>
</Style.Triggers>
</Style>
</RadioButton.Style>

</RadioButton>

当前您设置了本地值。由于依赖属性值优先级,样式无法覆盖它。

最新更新