简体   繁体   English

单选按钮和图像

[英]RadioButton and image

I try to set up images instead of bullet for my RadioButtons. 我尝试为RadioButtons设置图像而不是项目符号。 I've 10 RadioButtons but I can't setup my FishImg for all. 我有10个RadioButton,但无法全部设置FishImg。 Only the rb I clicked just before displays the imgFish. 只有我刚刚单击的rb才显示imgFish。

My Style 我的风格

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

My XAML 我的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" />

Finally I found it today even if I was pretty sure I tried it yesterday... may be tired. 终于我今天找到了,即使我很确定昨天也尝试过...可能会很累。

Here a working solution 这是一个可行的解决方案

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM