在WPF窗口中禁用除一个子控件外的所有控件 [英] Disabling all but one child control in a WPF window

查看:343
本文介绍了在WPF窗口中禁用除一个子控件外的所有控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的窗口上有一堆控件。其中之一是刷新按钮,它在后台线程上执行繁琐的任务。

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.

当用户单击刷新按钮时,我将光标置于等待状态(沙漏)状态并禁用整个窗口- Me.IsEnabled = False

When the user clicks the refresh button, I put the cursor in a wait (hourglass) status and disable the whole window -- 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?

推荐答案

您可以将所有控件放在一个面板(网格,StackPanel等)中,然后将取消按钮保留在另一面板中。然后设置另一个面板的IsEnabled属性。

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.

实际上,这可能会引入多个其他面板。

In practice, this will probably introduce more than one additional panel.

例如,如果您有一个StackPanel按钮,则可以添加一个附加的StackPanel:

For example, if you had a StackPanel of buttons, you can add an additional 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;

这篇关于在WPF窗口中禁用除一个子控件外的所有控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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