PropertyChanged event always null

68,325

Solution 1

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;

Solution 2

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()
}

Solution 3

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!

Solution 4

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.

Solution 5

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
    ***
}
Share:
68,325
Dave
Author by

Dave

Updated on July 09, 2022

Comments

  • Dave
    Dave almost 2 years

    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?

  • Dave
    Dave over 14 years
    I made the 2 changes as you suggested Ian, PropertyChanged is still evaluating as NULL. Kinda odd, I know this should work! I edited the code to reflect the changes.
  • Gusdor
    Gusdor over 13 years
    What, Singleton? What?! Never read so much rubbish in my life. The binding system will subscribe to the correct instance's event. If not WPF would just not work. Consult any beginners MVVM article for a working best-practice demonstration of how wrong you are.
  • Scott Nimrod
    Scott Nimrod over 13 years
    This is speaking from my own experience from trouble-shooting. I had more than one instance of my view model at run-time. Thus, this update worked for me. Thanks for your input though.
  • Scott Nimrod
    Scott Nimrod over 13 years
    Also, ensure that all the data is loaded (i.e. Loaded event).
  • danielpops
    danielpops about 13 years
    The reason that setting this.DataContext = testMessage; makes it work is that per my answer, that effictively establishes a subscription the the event that you wish to fire, hence it is no longer null.
  • eran otzap
    eran otzap over 12 years
    @IanR i don't think it would be null , because of that, it just wouldn't update the property he states that it doesn't even get a chance to update since it's null
  • lznt
    lznt almost 9 years
    I think Scott and Gusdor are talking about different staff. Scott is talking about the the ViewModel, which indeed should be the same all the time, while Gusdor is talking about the Model, which surely can be any number of objects. Scott indeed inspired me on debugging a similar situation. Thanks.
  • rfreytag
    rfreytag about 8 years
    I believe that the "check for null" approach is now the preferred approach. I did try initializing the event handler as you did in your first case and, true, the event was no longer null but the INotifyPropertyChanged event didn't trigger an update to the List<T> I was using. The fix was to use INotifyCollectionChanged (see: stackoverflow.com/questions/37329991/… for my experience with this problem).
  • N_tro_P
    N_tro_P over 6 years
    This answer has terrible grammar and does not offer a structured answer. You are proposing that the person monitor from the view loading, which is complex and will likely confuse them even more. If this is really your method of troubleshooting a null Prop event handler, you should provide methodology not an example of you doing it.