views:

49

answers:

2

I was pulling out my hair trying to figure out why the normal Dispatcher.Invoke command did not work for redrawing my window, but now I the issue seems to be related to the content being disabled. I'm using the Dotnet 4.0 full framework.

If i use

private void DoSomething()
{
  HandleBusyEnableDisable(false);
  DoSomethingThatKeepsItBusy();
  HandleBusyEnableDisable(true);
}

private void HandleBusyEnableDisable(bool enabling)
{
  Cursor = enabling ? Cursors.Arrow : Cursors.Wait; 
  CanvasFunctions.IsEnabled = enabling;
  CanvasRight.IsEnabled = enabling;
  CanvasRight.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Render, EmptyDelegate);

I see the cursor change but the content does not look disabled. If i add

CanvasRight.Opacity = enabling ? 1 : .5;

then i think it works, sometimes. Is there something else i can do?
The task that's running is validating user entered data, so its much easier to run on the GUI thread. This shouldn't be that hard.

+1  A: 

Not all controls visually represent being disabled, you may note that they wont be usable from the client side.

To be frank you should probably just implement INotifyChanged and bind the enabled field to the property. This will make sure the controls enable/disable properly as the Binding Framework will do the correct dispatches as IsEnabled I believe is registered with AffectsRender which will invalidate the control's visual state and force a re-draw.

Also you should use a Style to adjust the visual state of the Control:

<Style TargetType="Grid">
  <Style.Triggers>
    <DataTrigger Property="IsEnabled" Value="False">
       <Setter Property="Opacity" Value="0.5" />
    </DataTrigger>
  </Style.Triggers>
</Style>
Aren
A: 

If you're running the long running validation operation on the UI thread, you're locking up the UI and it can't update Visuals until the validation is complete. By that time you've already re-enabled the controls. If the operation is long enough that the user would notice it should be done on a background thread.

John Bowen
Why can't i force a synchronous refresh like winforms?
rediVider