我在创建的表单中启用和禁用按钮时遇到问题。
如果我从事件处理程序切换按钮的状态,它们会正常禁用。
private void btnBlock_Click(object sender, RoutedEventArgs e)
{
blockWizard();
}
private void blockWizard()
{
txtBackupName.IsEnabled = false;
txtSourceLocation.IsEnabled = false;
txtDestLocation.IsEnabled = false;
btnSourceBrowse.IsEnabled = false;
btnDestBrowse.IsEnabled = false;
chkRunJobNow.IsEnabled = false;
txtPassword.IsEnabled = false;
btnCreateBackup.IsEnabled = false;
}
如果我从方法中调用我的阻塞方法,它就不起作用。
private void SomeEvent()
{
SomeOtherEvent();
blockWizard();
}
如果发生这种情况,我们将不胜感激。
答案 0 :(得分:0)
我怀疑你的SomeEvent
方法是从UI线程以外的线程执行的。如果是这种情况,那么您无法从其他线程更新UI。
您可以添加以下内容:
private delegate void blockWizardHandler();
private void blockWizard()
{
if (Dispatcher.CheckAccess())
{
// We are on the UI thread, update the button states
txtBackupName.IsEnabled = false;
txtSourceLocation.IsEnabled = false;
txtDestLocation.IsEnabled = false;
btnSourceBrowse.IsEnabled = false;
btnDestBrowse.IsEnabled = false;
chkRunJobNow.IsEnabled = false;
txtPassword.IsEnabled = false;
btnCreateBackup.IsEnabled = false;
}
else
{
// We are not on the UI thread, invoke delegate to re-run this method on UI thread
Dispatcher.Invoke(new blockWizardHandler(blockWizard), null);
}
}