Disabling all but one child control in a WPF window

柔情痞子 提交于 2019-12-03 20:42:44

问题


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.

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?


回答1:


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.

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;



回答2:


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.




回答3:


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.

As Donnelle mentioned You can setup multi binding with a converter. Here are a couple examples you can refer to. WPF MultiBinding with Converter Implementing Parameterized MultiBinding Sample



来源:https://stackoverflow.com/questions/273809/disabling-all-but-one-child-control-in-a-wpf-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!