Reputation: 1095
I have the following (abbreviated) xaml:
<TextBlock Text="{Binding Path=statusMsg, UpdateSourceTrigger=PropertyChanged}"/>
I have a singleton class:
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";
I cannot get the textblock to display the test message. When I monitor the code through debug, the PropertyChanged is always null. Any ideas?
Upvotes: 45
Views: 76235
Reputation: 1113
Not to the questioner, but to the searchers on the Internet: I had the same problem that my
public event PropertyChangedEventHandler? PropertyChanged;
was always null!
Since I have built a class hierarchy, I have implemented the interface itself "INotifyPropertyChanged" in the top class, but I forgot to specify the interface in the class definition:
public class PropertyChangedBase : INotifyPropertyChanged // <-- these here!
Now I added INotifyPropertyChanged to the class definition and the PropertyChanged event is created.
Upvotes: 2
Reputation: 129
To complete the story, I also find if the bound property is static and the PropertyChanged event is not static, it will be null. A simple fix is to declare another static event so you have both as below:
public event PropertyChangedEventHandler PropertyChanged;
public static event PropertyChangedEventHandler StaticPropertyChanged;
Call PropertyChanged?.Invoke()
or StaticPropertyChanged?.Invoke()
according to whether the target property is static.
Upvotes: 0
Reputation: 491
I had a similar issue, neither solutions above helped me. All I needed to do was to use the built c# Propertychanged. Beforehand I have implemented propertyChanged (by an accident) and it pointed at nothing.
Upvotes: -1
Reputation: 51
in my case this make it to work:
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this; // this row fixed everything
}
****
Some code here with properties etc
***
}
Upvotes: 5
Reputation: 11
I also have seen the PropertyChanged event be null when I have existing data assigned to the control's data bound property:
<TextBlock Name="CarTireStatus" Text="{Binding TireStatus}" >Bad Text!</TextBlock>
Where as this works:
<TextBlock Name="CarTireStatus" Text="{Binding TireStatus}" ></TextBlock>
Upvotes: 1
Reputation: 1061
Just in case: I had a similar problem but my mistake was that class which implemented INotifyPropertyChanged was private. Making it public resolved my case.
Upvotes: 5
Reputation: 41
Another point - for PropertyChanged to be null make sure you bind the object to the DataContext and then set the Path instead of directly assigning the property to the UI field.
Upvotes: 1
Reputation: 7709
Your OnPropertyChanged string must exactly match the name of the property as it's case sensitive.
Try changing
OnPropertyChanged("StatusMsg");
to
OnPropertyChanged("statusMsg");
Update: Also - just noticed that you're binding to StatusMsg (capital 'S'); so the control was not binding to the property, which is another reason why it wasn't updating!
Upvotes: 10
Reputation: 731
I ran into this today and wasted some time on it, and eventually figured it out. I hope this helps save you and others some time.
If there are no subscribers to your event, and you simply declared the events as:
public event EventHandler SomeEventHappened;
Then null reference is expected. The way around this is to declare as follows:
public event EventHandler SomeEventHappened = delegate { };
This will ensure that it is not a null reference when you call as
SomeEventHappened()
Another pattern i've seen is to not initialize to delegate {} and instead check for null:
var eventToRaise = SomeEventHappened;
if( eventToRaise != null )
{
SomeEventHappened()
}
Upvotes: 16
Reputation: 945
If you follow all instructions, verifying your property name is correct, that it is correctly being assigned a new value, you are using a singleton to guarantee one instance of you view model, and you have successfully assigned your DataContext in the UI - make sure that whatever is forcing your property to update is done after the visual tree has been completed, i.e. move the refresh of your property to a button, rather than say the Loaded event of your window. I say this because I had the same issue, and found that when I refreshed my view model data property from my Infragistics NetAdvantage ribbon window's Loaded event, my PropertyChanged event was always null.
Upvotes: -3
Reputation: 11570
There is a couple of items to check for when observing the PropertyChanged event object as null.
Ensure the property name passed in as an argument when raising the event actually matches the name of the property you are targeting.
Ensure that you are using only one instance of the object that contains the property you are binding to.
For item number two, this can be done by simply placing a break point on the class constructor for the object that harbors the property that is being bound. If the breakpoint is triggered more than once, then you have a problem and need to resolve the number of instances of objects to only one instance that your runtime invokes via XAML.
Thus, it's better to implement that class as a singleton pattern so that you can ensure just one instance of the object at runt-time.
Upvotes: 0
Reputation: 1095
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;
Upvotes: 23