WPF按钮对启用和禁用状态使用不同的图标

时间:2014-01-03 18:34:20

标签: wpf button

如何根据启用状态为我的按钮指定不同的图标。还不太熟悉Style setters。

XAML按钮:

        <Button 
            x:Name="DownloadNewestEpisodes" 
            Width="90" 
            Margin="5,5"
            ToolTip="Download newest episodes"
            ToolTipService.ShowOnDisabled="True"
            Command="w:MainWindow.DownloadNewestEpisodesCommand"
            CommandParameter="{Binding ElementName=TvShowsTreeView, Path=SelectedItem}">

            <StackPanel>
                <Image Source="../icons/import.png" />
            </StackPanel>
        </Button>

如何在

之间切换
<Image Source="../icons/import.png" />

<Image Source="../icons/import_disabled.png" />

基于按钮的启用状态?

一点点帮助会很棒!

2 个答案:

答案 0 :(得分:5)

您可以使用DataTrigger

<Button...
    >
    <StackPanel>
        <Image>
            <Image.Style>
                <Style TargetType="Image">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" Value="True">
                            <Setter Property="Source" Value="..." />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" Value="False">
                            <Setter Property="Source" Value="..." />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Image.Style>
        </Image>
    </StackPanel>
</Button>

答案 1 :(得分:2)

在setter中设置默认图像并使用 DataTrigger切换到其他值(如果IsEnabled设置为False):

<Button>
   <Image>
      <Image.Style>
         <Style TargetType="Image">
            <Setter Property="Source" Value="Images/a.png"/>
              <Style.Triggers>
                 <DataTrigger Binding="{Binding IsEnabled,
                             RelativeSource={RelativeSource Mode=FindAncestor, 
                                                          AncestorType=Button}}"
                              Value="False">
                    <Setter Property="Source" Value="Images/c.png"/>
                 </DataTrigger>
              </Style.Triggers>
         </Style>
     </Image.Style>
  </Image>
</Button>