使用ICommand的按钮不会被禁用吗? [英] Button using ICommand does not get disabled?

查看:128
本文介绍了使用ICommand的按钮不会被禁用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的wpf-mvvm应用程序中有一个按钮控件.

I have a button control in my wpf-mvvm application.

我使用ICommand属性(在viewmodel中定义)将按钮click事件绑定到viewmodel.

I use an ICommand property (defined in viewmodel) to bind the button click event to viewmodel.

对于ICommand实现(RelayCommand),我具有-> execute和canexecute参数.

I have -> execute and canexecute parameters for my ICommand implementation (RelayCommand).

即使CanExecute为假...按钮也不被禁用...当按钮内容为图像时

Even if CanExecute is false...button is not disabled...WHEN button CONTENT is IMAGE

但是,当按钮内容为文本时,.enable/disable可以正常工作.

But, when button content is text..enable/disable works fine.

<Button DockPanel.Dock="Top" 
                        Command="{Binding Path=MoveUpCommand}">
                    <Button.Content>
                        <Image Source="/Resources/MoveUpArrow.png"></Image>
                    </Button.Content>
                    <Style>
                        <Style.Triggers>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Opacity" Value=".5" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Button>

推荐答案

按钮确实被禁用,只是它不影响图像的渲染. 您将必须编写一种样式的触发器,以将图像的不透明度更改为.5,这样您将获得禁用按钮的理想效果,如下所示:

Button does get disabled, its just that it doesn't affect rendering of the image. You will have to write a trigger in the style which changes the opacity of the image to .5 and you'll get the desired effect of button disabled like so:

<Style x:Key="imageButton" TargetType="Button">
        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Opacity" Value=".5" />
            </Trigger>
        </Style.Triggers>
</Style>

这篇关于使用ICommand的按钮不会被禁用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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