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