wpf - 检查后更改单选按钮的图像

标签 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个单选按钮,我在单选按钮上放置了图像,我的要求是我想在选中后更改单选按钮的图像,请为此提供帮助...

最佳答案

您可以为此使用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为true时更改该控件的IsChecked属性

编辑

如果您还想在鼠标悬停时“突出显示”,则可以添加DropShadowEffect和另一个Trigger,这一次是IsMouseOver为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>

关于wpf - 检查后更改单选按钮的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30987136/

相关文章:

xaml - .NET MAUI 创建方形 SKCanvasView

c# - ObservableCollection (ViewModel) 中的 MVVM ObservableCollection

.net - 带参数的 RelayCommand 抛出 MethodAccessException

WPF,让垂直拉伸(stretch)按预期工作!

c# - 在代码隐藏中添加网格

c# - 如何将 System.Windows.Controls 类型转换为 System.Windows.Forms wpf

c# - 在 .getJSON 中传递数据。可能的?如何?

c# - 'tag list'的WPF输入法

c# - 按照 MVVM 设计模式从 ViewModel 访问 this.content

wpf - 尝试绑定(bind)到 DataTrigger 内的枚举,为什么它不起作用?