6

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.

Maya
  • 1,414
  • 5
  • 22
  • 43
  • Your code looks OK, I would try `Data.Tables.FactoryItems.Symbol = "test";` right after the `+=` line. With a Debugger active. – H H Feb 20 '11 at 18:37

4 Answers4

5

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.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Hi @Slaks would you be able to provide me a short example on how to do that for the code above? – Maya Feb 20 '11 at 19:07
3

Until someone subscribes to the event, it will remain null.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • @devdigital @marc-gravell I have updated the code to show the subscription bit, I'm not using WPF or XAML, its just a class in the business layer that needs to advise a method of the FactoryItem static "table" changes in order to run another method to get updates from another source. – Maya Feb 20 '11 at 16:36
1

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 += ....

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Ah thank you so much! I was binding to a Linq enumerable of anonymous type and could not for the life of me figure out why it wasn't binding to my `Combobox` `ItemsSource`. I forgot the `.ToList()` on the end. Took me so long u_u – Jason Ridge Feb 29 '12 at 13:39
0

Are you just setting the Symbol property value in code and debugging from there? In which case, nothing may have subscribed to the PropertyChanged event.

If you are doing XAML development, then try setting the DataContext for a XAML element to be an instance of your FactoryItems type, and then bind the Symbol property to a Text property on a TextBox (for example), and you will find the XAML binding engine has subscribed to the PropertyChanged event, so it won't be null.

Alternatively, just have another type manually subscribe to your FactoryItems PropertyChanged event to test things in code.

devdigital
  • 34,151
  • 9
  • 98
  • 120