C# 单选按钮和图像

C# 单选按钮和图像,c#,xaml,radio-button,toggle,C#,Xaml,Radio Button,Toggle,我试着为我的单选按钮设置图像而不是子弹。我有10个单选按钮,但我不能为所有设置我的FishImg。只有我刚才单击的rb才会显示imgFish 我的风格 <Style TargetType="RadioButton"> <Setter Property="Content" Value="{DynamicResource imgFish}"/> <Setter Property="Template"> <Setter.

我试着为我的单选按钮设置图像而不是子弹。我有10个单选按钮,但我不能为所有设置我的FishImg。只有我刚才单击的rb才会显示imgFish

我的风格

    <Style TargetType="RadioButton">
    <Setter Property="Content" Value="{DynamicResource imgFish}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type RadioButton}">
                <Grid>
                    <Image Source="Images/empty.png" Width="24" Height="24" />
                    <ContentPresenter/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsChecked" Value="True">
            <Setter Property="Content" Value="{DynamicResource imgHero}"/>
        </Trigger>
        <Trigger Property="IsChecked" Value="False">
            <Setter Property="Content" Value="{DynamicResource imgFish}"/>
        </Trigger>
    </Style.Triggers>
</Style>

我的XAML

                <RadioButton Grid.Column="2" Grid.Row="1" Name="hvP1" />
                <RadioButton Grid.Column="2" Grid.Row="2" Name="hvP2" />
                <RadioButton Grid.Column="2" Grid.Row="3" Name="hvP3" />
                <RadioButton Grid.Column="2" Grid.Row="4" Name="hvP4" />
                <RadioButton Grid.Column="2" Grid.Row="5" Name="hvP5" />
                <RadioButton Grid.Column="2" Grid.Row="6" Name="hvP6" />
                <RadioButton Grid.Column="2" Grid.Row="7" Name="hvP7" />
                <RadioButton Grid.Column="2" Grid.Row="8" Name="hvP8" />
                <RadioButton Grid.Column="2" Grid.Row="9" Name="hvP9" />
                <RadioButton Grid.Column="2" Grid.Row="10" Name="hvP10" />

今天我终于找到了它,即使我很确定我昨天试过了。。。我可能累了

这里有一个有效的解决方案

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Image Source="Images/on.png" x:Key="imgOn"/>
    <Image Source="Images/off.png" x:Key="imgOff"/>
    <Style TargetType="RadioButton">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RadioButton}">
                    <Grid>
                        <Image Source="Images/off.png" Width="32" Height="32"/>
                        <ContentPresenter/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsChecked" Value="True">
                <Setter Property="Content" Value="{DynamicResource imgOn}"/>
            </Trigger>
            <Trigger Property="IsChecked" Value="False">
                <Setter Property="Content" Value="{DynamicResource imgOff}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<StackPanel>
    <RadioButton Name="rb1" Width="32" Height="32" Margin="5"/>
    <RadioButton Name="rb2" Width="32" Height="32" Margin="5"/>
    <RadioButton Name="rb3" Width="32" Height="32" Margin="5"/>
    <RadioButton Name="rb4" Width="32" Height="32" Margin="5"/>
</StackPanel>


如果我读对了,您希望从一开始就显示鱼的图像。然后,选中复选框将根据复选框状态是否选中或取消选中,为您提供英雄或鱼。如果是这种情况,则必须使用fishImg初始化内容。Style.Trigger代码仅在单击您找到的复选框后执行。