在 WPF 窗口中禁用除一个子控件之外的所有控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/273809/
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
Disabling all but one child control in a WPF window
提问by Rob Sobers
I have a bunch of controls on my window. One of them is a refresh button that performs a cumbersome task on a background thread.
我的窗口上有一堆控件。其中之一是在后台线程上执行繁琐任务的刷新按钮。
When the user clicks the refresh button, I put the cursor in a wait (hourglass) status and disable the whole window -- Me.IsEnabled = False
.
当用户单击刷新按钮时,我将光标置于等待(沙漏)状态并禁用整个窗口 -- Me.IsEnabled = False
。
I'd like to support cancellation of the refresh action by letting the user click a cancel button, but I can't facilitate this while the whole window is disabled.
我想通过让用户单击取消按钮来支持取消刷新操作,但是当整个窗口被禁用时,我无法做到这一点。
Is there a way to do this besides disabling each control (except for the cancel button) one by one and then re-enabling them one by one when the user clicks cancel?
除了一个一个地禁用每个控件(取消按钮除外),然后在用户单击取消时一个个地重新启用它们之外,还有什么方法可以做到这一点?
回答by Abe Heidebrecht
You can put all the controls in one panel (Grid, StackPanel, etc.), and leave the cancel button in another panel. Then set the IsEnabled property of the other panel.
您可以将所有控件放在一个面板(Grid、StackPanel 等)中,而将取消按钮留在另一个面板中。然后设置另一个面板的 IsEnabled 属性。
In practice, this will probably introduce more than one additional panel.
在实践中,这可能会引入多个额外的面板。
For example, if you had a StackPanel of buttons, you can add an additional StackPanel:
例如,如果您有一个 StackPanel 按钮,您可以添加一个额外的 StackPanel:
<StackPanel Orientation="Horizontal">
<StackPanel x:Name="controlContainer" Orientation="Horizontal">
<!-- Other Buttons Here -->
</StackPanel>
<Button Content="Cancel" />
</StackPanel>
Then, you would do the following to disable everything but the cancel button:
然后,您将执行以下操作以禁用除取消按钮之外的所有内容:
controlContainer.IsEnabled = false;
回答by DJR
I also wanted the user to be able to cancel out of loading. I found a lovely solution.
我还希望用户能够取消加载。我找到了一个可爱的解决方案。
foreach (Control ctrl in this.Controls)
ctrl.Enabled = false;
CancelButton.Enabled = true;
This also allows the main window to be selected and moved unlike this.Enabled = false;
which completely locks up the window.
这也允许选择和移动主窗口,this.Enabled = false;
这与完全锁定窗口不同。
回答by Aaron Fischer
You can data bind each controls IsEnabled property to your custom boolean dependency property that signals when your application is in lock down. Just don't bind the cancel button.
您可以将每个控件的 IsEnabled 属性数据绑定到您的自定义布尔依赖属性,该属性在您的应用程序处于锁定状态时发出信号。只是不要绑定取消按钮。
As Donnelle mentioned You can setup multi binding with a converter. Here are a couple examples you can refer to. WPF MultiBinding with ConverterImplementing Parameterized MultiBinding Sample
正如 Donnelle 所提到的,您可以使用转换器设置多重绑定。这里有几个例子,你可以参考。 WPF 多绑定与转换器实现参数化多绑定示例