I have the following code. Within the same block of code, I disable a control then enabled it, but it does not seem to work and remain the initial IsEnable state.
// Button never enable
public void Foo()
{
button1.IsEnabled = false
Thread.Sleep(3000);
button1.IsEnabled = true;
Thread.Sleep(3000);
button1.IsEnabled = false;
}
First of all: it isn’t that strange that the button is not enabled after the
Foomethod is done, as the last line disables the button.If you want to “see” the button’s enabled state change, you need to handle window messages before sleeping.
Thread.Sleepcauses the UI thread to sleep, so there will be no updates to the user interface. What you need to do is make the application handle all pending window messages.EDIT
Further explanation:
When the first
Thread.Sleepis called, the button is disabled (IsEnabledisfalse). Then, after waiting for 3 seconds, you set the button to enabled, and after that to disable it again. The button is actually in that state, but there is no visual feedback (as in: if you look at the form, you won’t see the button “blink”).This is because you are blocking the UI thread by calling
Thread.Sleep. This prevents the window from receiving window messages, which tell the window when to repaint itself or one of its children (the button doesn’t “know” it should be gray).So you need to give the window a chance to receive the messages and update its children. In Windows Forms applications, this was done with
Application.DoEvents, which processed all pending window messages.There’s no such thing in WPF as
Application.DoEvents, but the code I’ve posted emulates this by spwaning aThreadthat does nothing – but that little time between changing the button’s enabled state and blocking the UI thread is enough for the button control to repaint itself.