[size=medium]禁用按钮是要在整个事件结束后才会响应[/size]
public class WpfApplication
{
private static DispatcherOperationCallback exitFrameCallback = new DispatcherOperationCallback(ExitFrame);
public static void DoEvents()
{
DispatcherFrame nestedFrame = new DispatcherFrame();
DispatcherOperation exitOperation = Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, exitFrameCallback, nestedFrame);
Dispatcher.PushFrame(nestedFrame);
if (exitOperation.Status != DispatcherOperationStatus.Completed)
{
exitOperation.Abort();
}
}
private static Object ExitFrame(Object state)
{
DispatcherFrame frame = state as DispatcherFrame;
frame.Continue = false;
return null;
}
}
private void button_Click(object sender, RoutedEventArgs e)
{
button.IsEnabled = false;
WpfApplication.DoEvents();
i++;
button.Content = i.ToString();
System.Threading.Thread.Sleep(2000);
button.IsEnabled = true;
}