选中后更改单选按钮的图像

时间:2015-06-22 18:23:02

标签: wpf xaml mvvm

<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个单选按钮我把图像放在单选按钮上,我的要求是我想在选中后更改单选按钮的图像,请帮我这个......

1 个答案:

答案 0 :(得分:3)

您可以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为真时,为Source提供一些名称并更改该控件的IsChecked属性

修改

如果你想在鼠标结束时“突出显示”那么你可以添加例如DropShadowEffect和另一个Trigger,这次是IsMouseOver为真

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