简体   繁体   English

如何使用C#从WPF表单禁用鼠标和键盘读取操作

[英]How to disable mouse and keyboard read operation from WPF form using C#

我想在一个文本框中显示所有有关客户的信息,所有这些都在文本框中显示,而其他控件则不能执行写操作,而只有一个退出按钮。

One answer, as unfortunate as it is, is to add a bool property to your view model/code behind and to bind that to the IsEnabled property of every control that you want to be disabled and then to set that property to false when you want to disable them. 不幸的是,一个答案是在后面的视图模型/代码中添加一个bool属性,并将其绑定到要禁用的每个控件的IsEnabled属性,然后在需要时将该属性设置为false禁用它们。

<Button Content="Something" IsEnabled="{Binding AreControlsEnabled}" />
<Button Content="Something else" IsEnabled="{Binding AreControlsEnabled}" />
<Button Content="Another thing" /> <!--  <<< Will remain enabled   -->

In view model/code behind: 在后面的视图模型/代码中:

AreControlsEnabled = false; // <<< Disables controls

If you have a panel for the input fields, then disable that panel and enable just the button you want. 如果您有一个用于输入字段的面板,则禁用该面板并仅启用所需的按钮。

For example, move all your input controls to inside some panel (say StackPanel ), and keep your button outside the panel. 例如,将所有输入控件移到某个面板内(例如StackPanel ),并将按钮保持在面板外。 Now disable the stackPanel to get all your input controls disabled. 现在禁用stackPanel以禁用所有输入控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM