c# - WPF 中按钮启用和禁用状态的不同图像

标签 c# wpf image button

我想在下面的代码中根据按钮的状态更改按钮的图像,即使用不同的图像来启用和禁用状态。

<Button CommandParameter="Open" >
    <StackPanel Orientation="Horizontal" >
        <Image Source="../icons/big/open.png" Width="20" Height="20"></Image>
    </StackPanel>
</Button>

谢谢。

最佳答案

您可以像这样使用带有触发器的样式:

<Style TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <StackPanel Orientation="Horizontal" >
                    <Image Name="PART_Image" Source="path to normal image" />
                </StackPanel>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Source" Value="path to mouse over image" TargetName="PART_Image"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="Source" Value="path to pressed image" TargetName="PART_Image"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Source" Value="path to disabled image" TargetName="PART_Image"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

关于c# - WPF 中按钮启用和禁用状态的不同图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1532125/

相关文章:

image - 从车牌上删除多余的像素/线

javascript - 2 AngularJS 图像获取中单个资源的 GET 方法形成

php - 在 SSL 中如何优雅地处理重定向到 http 的 https URL 的 IMG src

wpf - 关于 ViewModel 对简单状态的责任的 MVVM 设计选择

WPF 触发器未按预期工作

c# - Visual Studio 无法附加到 Unity,为什么?

XML 序列化 "There is an error in XML document (2, 2)"的 C# 错误

c# - 如何从 FileStream 报告进度

c# - 如何在 C# 中调用 Web 服务方法

c# - 从 C# 到 VB.Net 的 Lambda 表达式