PropertyChanged 事件始终为空 [英] PropertyChanged event always null
本文介绍了PropertyChanged 事件始终为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下(缩写)xaml:
I have the following (abbreviated) xaml:
<TextBlock Text="{Binding Path=statusMsg, UpdateSourceTrigger=PropertyChanged}"/>
我有一个单例类:
public class StatusMessage : INotifyPropertyChanged
{
private static StatusMessage instance = new StatusMessage();
private StatusMessage() { }
public static StatusMessage GetInstance()
{
return instance;
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string status)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(status));
}
}
private string statusMessage;
public string statusMsg
{
get
{
return statusMessage;
}
set
{
statusMessage = value;
OnPropertyChanged("statusMsg");
}
}
}
在我的主窗口构造函数中:
And in my main window constructor:
StatusMessage testMessage = StatusMessage.GetInstance();
testMessage.statusMsg = "This is a test msg";
我无法使用文本块来显示测试消息.当我通过调试监视代码时,PropertyChanged 始终为空.有什么想法吗?
I cannot get the textblock to display the test message. When I monitor the code through debug, the PropertyChanged is always null. Any ideas?
推荐答案
谢谢 Jerome!一旦我设置了 DataContext,它就开始正常工作了!出于测试目的,我将以下内容添加到主窗口构造函数中:
Thanks Jerome! Once I set the DataContext it started working as it should! I added the following to the main window constructor for testing purposes:
this.DataContext = testMessage;
这篇关于PropertyChanged 事件始终为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文