Why is my "Event" always zero?

I am trying to hook up a new event, but for some reason "Modified" always evaluates to null

    public class MyTreeViewItem : INotifyPropertyChanged

{
        private MyTreeViewItem _parent;

        public MyTreeViewItem(MyTreeViewItem parent)
        {
            _parent = parent;
        }

        private bool _checked;
        public bool Checked
        {
            get
            {
                return _checked;
            }
            set
            {
                if (value != _checked)
                {
                    _checked = value;
                    OnChanged("test");
                    OnPropertyChanged("Checked");
                }
            }
        }

        public event EventHandler Changed;

        public ObservableCollection<MyTreeViewItem> Children { get; set; }

    // Invoke the Changed event; called whenever list changes
    protected virtual void OnChanged(string test)
    {
        if (Changed != null)
            Changed(this, null);
    }

Signing Code (PropertyChanged Works, Changed is not)

_playgroupTree = new MyTreeViewItem(null);
AddChildNodes(4, ref _playgroupTree);
_playgroupTree.Changed += new EventHandler(_playgroupTree_Changed);
_playgroupTree.PropertyChanged += new PropertyChangedEventHandler(_playgroupTree_PropertyChanged);

Really strange, because I also implement INotifyPropertyChanged (which works), and this code is almost exactly the same (I tried to use the same type of division, but it still doesn't work.

I use this site as a link http://msdn.microsoft.com/en-us/library/aa645739%28VS.71%29.aspx

+5
source share
4 answers

Well, you did not specify the code that signed up for this event. Where is your code like this:

YourClass yc = new YourClass();
yc.Changed += SomeHandler;

? , , , .

+17

Changed, null.

Changed += (s, e) => Console.WriteLine("received Changed event");
if (Changed != null) Console.WriteLine("now Changed is not null");
+3

you missed this:

List.Changed += new ChangedEventHandler(ListChanged);
+2
source

Have you assigned a value, or more specifically, an event handler, anywhere? It must be empty if it is not assigned a value ...

+1
source

All Articles