检查RadioButton时如何使用触发器更改RadioButton的StackPanel背景

时间:2016-03-20 02:43:02

标签: wpf xaml windows-phone-8

我有多个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来避免代码重复并简化修改?

1 个答案:

答案 0 :(得分:2)

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

<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>