0

I was reviewing this stack overflow post and tested it, and it worked as expected. However, I notice as soon as I assign a new object to the local variable the binding no longer works.

I have 2 questions:

1) There has to be a better way, I can't imagine going through and modifying all my properties and changing from a simple 1 line of code public string Title2 { get; set; } to some lengthy getter/setter property that calls some method.

2) Do I have to rebind every time I create a new instance of the object?

Userform:

MyClass myClass;
textbox1.DataBindings.Add("Text", this.myClass, "MyName");

Object

public class MyClass : INotifyPropertyChanged
{
   private string _myName;

   public string MyName
   {
       get { return _myName; }
       set
       {
          if( _myName != value )
          {
              _myName = value;
              OnPropertyChanged("MyName");
          }
       }
   }

   public event PropertyChangedEventHandler PropertyChanged;

   private void OnPropertyChanged(string propertyName)
   {
       if( PropertyChanged != null )
           PropertyChanged( this , new PropertyChangedEventArgs(propertyName) );
   }       
}

But as soon as you create a new instance of myClass in the userform, the binding no longer works...

myClass = new MyClass();
Community
  • 1
  • 1
Kairan
  • 5,342
  • 27
  • 65
  • 104

1 Answers1

0

That's exactly how it's supposed to work... that binding is still working but on the previous instance of the class and yes, you would have rebind every time you create a new object.