WPF 启用/禁用控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2352850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:32:09  来源:igfitidea点击:

WPF enable/disable controls

wpfcontrols

提问by TheWommies

When I disable a control in WPF, like say a menu item like

当我禁用 WPF 中的控件时,比如说一个菜单项

MenuItem aMenuItem = ...
aMenuItem.IsEnabled = false;

the text in the MenuItem is still active, that is it is not grayed out as you would expect when items are disabled.

MenuItem 中的文本仍然处于活动状态,也就是说,当项目被禁用时,它不会像您期望的那样变灰。

Is there a simple way to do this not only for Menu items but for any WPF control?

是否有一种简单的方法不仅可以针对菜单项而且可以针对任何 WPF 控件执行此操作?

回答by Aran Mulholland

yes by using commands. MenuItems and Buttons have a command property. a Command is an implementation of the ICommand interface which has a method called CanExecute. When can execute is called if it returns true the menutitem or button is enabled, otherwise it is greyed out.

是通过使用命令。MenuItems 和 Buttons 有一个 command 属性。命令是 ICommand 接口的一个实现,它有一个名为 CanExecute 的方法。当 can execute 被调用时,如果它返回 true 菜单项或按钮被启用,否则它是灰色的。

MSDN Command overview

MSDN 命令概述

nice simple tutorial on setting up commands

关于设置命令的简单教程

google search :)

谷歌搜索 :)

回答by someone

You can also just set the opacity of the control to 0.5 , to get the "grayed out" effect

您也可以将控件的不透明度设置为 0.5 ,以获得“变灰”效果