c# - 单选按钮和图像

标签 c# xaml radio-button toggle

我尝试为我的单选按钮设置图像而不是项目符号。我有 10 个 RadioButtons,但我无法为所有按钮设置 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>

关于c# - 单选按钮和图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16573358/

相关文章:

angular - 取消选择单选按钮 Angular 2?

c# - 从 C# ASP WEB 应用程序测试 SQL 连接

c# - 带有浏览按钮作为用户控件的 WPF 路径文本框

c# - 在客户端点击隐藏div

.net - WPF 绑定(bind)RelativeSource 问题

c# - 在没有 ICommand 的情况下将事件绑定(bind)到 ViewModel 的方法

c# - Windows Phone 8.1 MessageDialog 结果

c# - 如何绑定(bind)到 WPF 中动态创建的单选按钮?

c# - 如何使类通用以在c#中传递动态属性类

javascript - 如何通过单击单选按钮来切换标签的类别。需要 Vanilla JS 而不是 JQuery