我正在尝试使用triggers
来修改我的控件的属性。我有一个RadioButton
和一个Border
,我想修改边框Background
RadioButton
被选中。所以这是我的代码:
<Border BorderBrush="{DynamicResource MaterialDesignDivider}">
<RadioButton IsChecked="{Binding Erase_IsSelected}" Content="E">
<RadioButton.Resources>
<Style TargetType="{x:Type RadioButton}">
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Background" Value="{DynamicResource MaterialDesignSelection}" />
</Trigger>
</Style.Triggers>
</Style>
</RadioButton.Resources>
</RadioButton>
</Border>
这似乎不起作用,我该如何解决?谢谢!
答案 0 :(得分:3)
对Style
绑定Border
的{{1}}属性的IsChecked
使用RadioButton
:
<Border BorderBrush="{DynamicResource MaterialDesignDivider}">
<Border.Style>
<Style TargetType="Border">
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked, ElementName=rb}" Value="True">
<Setter Property="Background" Value="{DynamicResource MaterialDesignSelection}" />
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<RadioButton x:Name="rb" IsChecked="{Binding Erase_IsSelected}" Content="E" />
</Border>
RadioButton
样式无法更改Border
的属性。
答案 1 :(得分:-1)
在xaml中添加 OnChecked 事件:
<RadioButton Checked="Radiobutton_OnChecked" Content="E">
<RadioButton.Resources>
<Style TargetType="{x:Type RadioButton}">
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Background" Value="{DynamicResource MaterialDesignSelection}" />
</Trigger>
</Style.Triggers>
</Style>
</RadioButton.Resources>
</RadioButton>
这将在您的.cs中创建一个函数,您可以在其中更改背景:
private void Radiobutton_OnChecked(object sender, RoutedEventArgs e)
{
RadioButton rdb = (RadioButton) sender;
rdb.Background = new SolidColorBrush(Colors.Green);
}