I have the following code and it works fine.
public partial class MainWindow : Window { Person person; public MainWindow() { InitializeComponent(); person = new Person { Name = "ABC" }; this.DataContext = person; } private void Button_Click(object sender, RoutedEventArgs e) { person.Name = "XYZ"; } } class Person: INotifyPropertyChanged { string name; public string Name { get { return name; } set { name = value; OnPropertyChanged("Name"); } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string strPropertyName) { if(null != PropertyChanged) { PropertyChanged(this, new PropertyChangedEventArgs(strPropertyName)); } } }
When I create a person object in the MainWindow constructor, it will assign a value for the Name property to the person, then the PropertyChanged event is NULL .
If the same "person" property of the "Name" class is assigned in the Button_Click event, the "PropertyChanged" event is NOT NULL and points to OnPropertyChanged.
My question is, how is the "PropertyChanged" event assigned to the OnPropertyChanged method?
Thanks in advance.
source share