我正在尝试使用MVVM在WPF中创建一个简单的数字时钟。 我有一个带有绑定的标签。 后台代码很简单,每秒钟引发一个属性更改事件,但是我遇到了stackoverflow异常。 请问有人能帮忙吗?
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
private string _lblValue;
public string LabelValue
{
get
{
UpdateLabel();
return _lblValue;
}
set
{
_lblValue = value;
OnPropertyChanged(LabelValue);
}
}
private void UpdateLabel()
{
_lblValue = System.DateTime.Now.ToString();
//System.Threading.Thread.Sleep(2000);
OnPropertyChanged("LabelValue");
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propName));
}
}
}
Window
、Component
或其他UI类。 - Tseng