如何在选中 RadioButton 时使用触发器更改 RadioButton 的 StackPanel 背景

How to Use Trigger to Change RadioButton's StackPanel Background When the RadioButton is Checked

我有多个 RadioButton,其中一个是 Image 和一个用 StackPanel 包裹的 TextBlock 的组合

<RadioButton
    Tag="0"
    Grid.Column="0"
    Grid.Row="0"
    Name="rdbOutlook">
    <StackPanel>
        <Image Source="Resources/outlook.png"
            Stretch="Fill"
            Width="50"/>
        <TextBlock Text="Outlook/Hotmail" />
    </StackPanel>
</RadioButton>

我想在选中 RadioButton 时将 StackPanel 背景设置为这样的颜色。

如何使用 Window.Resources 来避免代码重复并简化修改?

您可以对包含 StackPanel 的所有 RadioButton 使用以下代码:

<Window.Resources>
    <Style TargetType="StackPanel">
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={x:Type RadioButton}}, Path=IsChecked}" 
                         Value="True">
                <Setter Property="Background" Value="Thistle" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>