18

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?

jpierson
  • 16,435
  • 14
  • 105
  • 149
Rob Sobers
  • 20,737
  • 24
  • 82
  • 111
  • I can't seem to use both Me.IsEnabled and Me.Cursor. http://stackoverflow.com/questions/762396/wpf-wait-cursor-with-backgroundworker-thread – Zack Peterson Apr 17 '09 at 22:08

3 Answers3

23

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;
Abe Heidebrecht
  • 30,090
  • 7
  • 62
  • 66
  • 1
    StackPanel doesn't have an IsEnabled property in WP8 – radders Sep 08 '14 at 15:47
  • 1
    Well, I have done 0 work with WP8, but I found the docs for the WP8 `StackPanel`: http://msdn.microsoft.com/en-US/library/windows/apps/windows.ui.xaml.controls.stackpanel.aspx I assume you're using that api, and not the Silverlight one, since Silverlight has an `IsEnabled` property. While you won't get the exact look of disabled controls, you could always set the `IsHitTestVisible="false"` to disable input on `controlContainer`. Otherwise, you could have a semi-transparent overlay that you make visible when you want to disable the other controls (and use a `Grid` as the outer panel). – Abe Heidebrecht Sep 09 '14 at 05:20
5

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.

derloopkat
  • 6,232
  • 16
  • 38
  • 45
DJR
  • 51
  • 1
  • 1
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

Aaron Fischer
  • 20,853
  • 18
  • 75
  • 116
  • The only problem I have w/ that is that the IsEnabled property on some of my controls is already data bound to a boolean property for other purposes. – Rob Sobers Nov 07 '08 at 22:34
  • 1
    You could use a multi-binding with a converter-- there are examples around for how to do a generic one with a parameter for AND or OR behaviours. – Donnelle Nov 08 '08 at 23:17