INotifyPropertyChanged PropertyChangedEventHandler event is always null
Solution 1
You're subscribing to the event on one particular instance.
That doesn't affect any other instances.
To see changes for all instances, you can use a static event.
Beware that any class that subscribes to the static event will stay in memory forever.
Solution 2
Until someone subscribes to the event, it will remain null
.
Solution 3
There are various types that will subscribe to this - most notably thing like DataGridView
(winforms, but similar in WPF etc) but only when data-bound (set DataSource
), and only if the list also supports binding - for example BindingList<T>
.
To test, simply subscribe to if yourself with PropertyChanged += ...
.
Maya
Updated on June 20, 2022Comments
-
Maya almost 2 years
I have implemented INotifyPropertyChanged to the following class
public class FactoryItems : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; string symbol; public string Symbol { get { return symbol; } set { symbol = value; OnPropertyChanged("Symbol"); } } public FactoryItems() { } protected void OnPropertyChanged(string name) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(name)); } } }
When the Symbol property changes the event fires with no problems but the PropertyChanged event is always null, this class gets instantiated once only, I placed a breakpoint on the constructor to make sure its the case.
In another class this is how I subscribe to it:
Data.Tables.FactoryItems = new Data.FactoryItems(); Data.Tables.FactoryItems.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(FactoryItems_SymbolChanged); void FactoryItems_SymbolChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { doSomething(); }
But the handler is always null as PropertyChanged is null. Any idea how to get this working?
Many thanks.