0

I have a class Manager on which I have implemented INotifyPropertyChanged. The class has a single nullable int property.

public class Manager : INotifyPropertyChanged
    {
        private int? managerid = 0;

        public int? Managerid
        {
            get
            {
                return managerid;
            }

            set
            {
                managerid = value;
                RaisePropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged([CallerMemberName]string prop = null)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }
    }

I have bind a single text box on my WinForm as:

Manager mgr = new Manager();
mgr.PropertyChanged += PropertyChanged_Mgr;
textbox.DataBindings.Add("Text", mgr, "Managerid");

Following is the hooked method:

static void PropertyChanged_Mgr(object sender, System.ComponentModel.PropertyChangedEventArgs e)

The problem is that that property change doesn't fire if the property is nullable. If I make the said property not nullable, it works fine. Any help will be highly appreciated

  • Did you try this google search? It seems to have a lot of promising results: https://www.google.com/#safe=active&q=.net+propertychanged+not+firing+on+nullable+property. Your title should be updated to reflect the problem more precisely, i.e. that it's *on nullable properties*. Also, the fact that it's winforms is irrelevant. and you should probably remove the tag and the mention of it in the title. Ultimately, though, this question will probably be closed as a dupe of another one. – rory.ap Apr 12 '17 at 11:09
  • Yes, I've been trying the google search for the last 2 days but didn't find any solution. The title might not be as precise as it should be but now I don't know how to edit it. – Nouman Butt Apr 12 '17 at 11:13
  • What about the google search i linked in my comment? – rory.ap Apr 12 '17 at 11:13
  • I've already visited most of the links before. I think you haven't read the exact problem I am facing. My property change is working fine if the property is not nullable, but it doesn't work for nullable one. – Nouman Butt Apr 12 '17 at 11:17
  • Now you said that WinForms is irrelevant in my query which is wrong. I tried the same code with WPF binding and it is working fine both for the nullable and not-nullable properties. – Nouman Butt Apr 12 '17 at 11:19
  • http://stackoverflow.com/questions/376361/best-way-to-databind-a-winforms-control-to-a-nullable-type – NDJ Apr 12 '17 at 11:58
  • Thank you, got the solution from the shared link. – Nouman Butt Apr 12 '17 at 12:46

0 Answers0