Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何使用触发器更改单选按钮';选中RadioButton时显示StackPanel背景_Wpf_Xaml_Windows Phone 8 - Fatal编程技术网

Wpf 如何使用触发器更改单选按钮';选中RadioButton时显示StackPanel背景

Wpf 如何使用触发器更改单选按钮';选中RadioButton时显示StackPanel背景,wpf,xaml,windows-phone-8,Wpf,Xaml,Windows Phone 8,我有多个单选按钮,其中一个单选按钮由图像和用StackPanel包装的文本块组成 <RadioButton Tag="0" Grid.Column="0" Grid.Row="0" Name="rdbOutlook"> <StackPanel> <Image Source="Resources/outlook.png" Stretch="Fill" Width="

我有多个单选按钮,其中一个单选按钮由图像和用StackPanel包装的文本块组成

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

您可以对包含
堆栈面板的所有
单选按钮使用以下代码:

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

您可以对包含
堆叠面板的所有
单选按钮使用以下代码:

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


你能解释一下为什么两次使用RelativeSource吗?Abdulkarim,你没看到吗?我还使用了
绑定
两次别问我,这是
Xaml
语法<代码>Xaml
的意思是»学习慢,忘记快,不理解«。你能解释一下为什么两次使用RelativeSource吗?Abdulkarim,你没看到吗?我还使用了
绑定
两次别问我,这是
Xaml
语法<代码>Xaml
意味着»学习慢,忘记快,不理解«。