WPF触发器无法正常工作

时间:2017-08-07 08:17:46

标签: c# wpf triggers

我正在尝试使用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>

这似乎不起作用,我该如何解决?谢谢!

2 个答案:

答案 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);
}