切换按钮-单击更改图标

时间:2018-11-08 15:17:20

标签: wpf xaml mahapps.metro

我在WPF中有一个应用程序。我正在使用entypo图标,并且将其中一个图标作为资源:

<Grid.Resources>
  <iconPacks:Entypo x:Key="PlayIcon" Width="50" Height="30" Kind="ControllerPlay"></iconPacks:Entypo>                              
</Grid.Resources>

假设我有两个这样的图标(播放/暂停图标),并且我想在用户单击ToggleButton时在它们之间进行切换。我想到了类似的方法,但不幸的是,它不起作用:

<ToggleButton>
   <Image>
      <Image.Style>
          <Style TargetType="{x:Type Image}">
             <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=ToggleButton}, Path=IsChecked}"
                 Value="true">
                    <Setter Property="Source"
                     Value="{StaticResource PauseIcon}" />
                 </DataTrigger>
                 <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=ToggleButton}, Path=IsChecked}"
                  Value="false">
                     <Setter Property="Source"
                      Value="{StaticResource PlayIcon}" />
                 </DataTrigger>
              </Style.Triggers>
            </Style>
       </Image.Style>
   </Image>
</ToggleButton>

有人可以告诉我我是否可以做到这一点(稍作修改)或为我指明正确的方向?

1 个答案:

答案 0 :(得分:1)

您不能将Source中的Image设置为PackIconEntypo。设置Content的{​​{1}}属性:

ToggleButton
相关问题