【发布时间】:2016-10-31 12:07:11
【问题描述】:
我对 WPF 相当陌生,并且仍在寻找一种简单的解决方案来暂时停用我的应用程序中的按钮。这是我正在做的事情:
按钮点击:
private void Calculate_Click(object sender, RoutedEventArgs e)
{
this.AreButtonsEnabled = false;
System.Threading.Thread.Sleep(1000);
this.AreButtonsEnabled = true;
}
XAML:
<Button x:Name="OtherButton" IsEnabled="{Binding AreButtonsEnabled}" Content="OtherButton" HorizontalAlignment="Left" Margin="165,152,0,0" VerticalAlignment="Top" Width="75"/>
绑定按预期工作,但如果我激活和停用此调用中的按钮,则停用不会完成。如何让 GUI 正确停用按钮?
编辑
通知事件(工作正常):
public partial class MainWindow : Window, INotifyPropertyChanged
{
private bool areButtonsEnabled;
public bool AreButtonsEnabled
{
get { return this.areButtonsEnabled; }
set { areButtonsEnabled = value; OnPropertyChanged(new PropertyChangedEventArgs("AreButtonsEnabled")); }
}
注意 我必须使用 .Net 3.5。
【问题讨论】:
-
你能发布你的“AreButtonsEnabled”属性是如何声明的吗?
-
这可能是因为 UI 在休眠之前没有时间对
AreButtonsEnabled设置为 false 做出反应。 -
我建议使用 ICommand 接口和 RelayCommand 等基本实现