选中后更改单选按钮的图像 [英] Change the image of the radio button after checked

查看:39
本文介绍了选中后更改单选按钮的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<StackPanel Name="StpAddDel" Orientation="Horizontal" HorizontalAlignment="Right" Margin="5">
   <RadioButton Name="rdbactive" GroupName="actinact" VerticalAlignment="Center" Margin="5,0" Width="50" Height="15" Foreground="Blue">
      <RadioButton.Style>
         <Style TargetType="{x:Type RadioButton}">
            <Setter Property="Template">
               <Setter.Value>
                  <ControlTemplate TargetType="{x:Type RadioButton}">
                     <Grid>
                        <Image Source="c:\image.png" Width="32" Height="32"/>
                        <ContentPresenter/>
                     </Grid>
                  </ControlTemplate>
               </Setter.Value>
            </Setter>
         </Style>
      </RadioButton.Style>
   </RadioButton>
   <RadioButton Name="rdbinactive" GroupName="actinact" VerticalAlignment="Center" Margin="5,0" Width="60" Height="15" Foreground="Blue">
      <RadioButton.Style>
         <Style TargetType="{x:Type RadioButton}">
            <Setter Property="Template">
               <Setter.Value>
                  <ControlTemplate TargetType="{x:Type RadioButton}">
                     <Grid>
                        <Image Source="c:\image.png" Width="32" Height="32"/>
                        <ContentPresenter/>
                     </Grid>
                  </ControlTemplate>
               </Setter.Value>
            </Setter>
         </Style>
      </RadioButton.Style>
   </RadioButton>
   <Button Name="BtnAdd"  Height="20" Width="20" Margin="5,0" Template="{StaticResource AddImgBtnTemplate}" />
   <Button Name="BtnDel" Height="20" Width="20" Margin="5,0" Template="{StaticResource DelImgBtnTemplate}" />
</StackPanel>

在上面的代码中,我有 2 个单选按钮,我将图像放在单选按钮上,我的要求是我想在选中后更改单选按钮的图像,请帮我解决这个......

In the above code i have 2 radio button i put images on radio buttons , My requirement is i want to change the image of the radio button after it is checked, Please help me with this......

推荐答案

你可以使用ControlTemplate.Triggers

<ControlTemplate TargetType="{x:Type RadioButton}">
   <Grid>
       <Image x:Name="PART_Image" Source="c:\image.png" Width="32" Height="32"/>
       <ContentPresenter/>
   </Grid>
   <ControlTemplate.Triggers>
       <Trigger Property="IsChecked" Value="True">
           <Setter TargetName="PART_Image" Property="Source" Value="new source"/>
       </Trigger>
   </ControlTemplate.Triggers>
</ControlTemplate>

Image 一些名称并在 IsChecked 为真时更改该控件的 Source 属性

Give Image some name and change Source property of that control when IsChecked is true

编辑

如果您还想在鼠标悬停时突出显示",那么您可以添加例如 DropShadowEffect 和另一个 Trigger,这次是 IsMouseOver 是真的

If you want to also "highlight" when mouse is over then you can add for example DropShadowEffect with another Trigger, this time when IsMouseOver is true

<Trigger Property="IsMouseOver" Value="True">
    <Setter TargetName="PART_Image" Property="Effect">
        <Setter.Value>
            <DropShadowEffect ShadowDepth="0" Color="Yellow" Opacity="0.5"/>
        </Setter.Value>
    </Setter>
</Trigger>

这篇关于选中后更改单选按钮的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆