不同的图像在WPF按钮启用和禁用状态 [英] different images for enable and disable states of a button in WPF

查看:140
本文介绍了不同的图像在WPF按钮启用和禁用状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下面根据其状态,即用于启用或禁用状态不同的形象来改变按钮的图像中的code。

I want to change the image of the button in the code below based on its state i.e. use different image for enable and disable state.

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

感谢您。

推荐答案

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

You can use a style with triggers like this:

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

这篇关于不同的图像在WPF按钮启用和禁用状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆