I'll try to explain.
Between the View (the graphical interface of the program (buttons, text fields, and so on.)) Using the so-called bunding (someone calls the binding, although this is a wrong pronunciation) a connection is established with the properties in the ViewModel. For example, we associate a property (ViewModel) with a text input field - public string Name { get; set; } public string Name { get; set; } public string Name { get; set; } using an entry in XAML (View) <TextBox Text="{Binding Name, Mode=TwoWay}" . In order to be able to notify the textbox that the data in the Name property has changed (for example, during any calculations) and the textbox "re-read" the data from Name in the set; block set; This variable needs to trigger the PropertyChanged event.
To do this, the ViewModel implement (inherit) from the standard INotifyPropertyChanged INotifyPropertyChanged . In your example, one of the following variants is used:
public event PropertyChangedEventHandler PropertyChanged; - Actually the announcement of the desired event.
public void OnPropertyChanged([CallerMemberName]string prop = "") is a helper method for triggering this event from the set; block set; .
[CallerMemberName] is an attribute of the parameter that allows you not to specify the name of the property to the programmer, the compiler will itself substitute the name of the property that calls the OnPropertyChanged method, i.e. You can call OnPropertyChanged("Name") or OnPropertyChanged() and then ("Name") compiler will replace you.
Further, in the method itself, the verification takes place; and if this event has subscribers? And if there are any (we have <TextBox Text="{Binding Name, Mode=TwoWay}" ), the event is called with transmission as arguments: the class that calls this event and the name of the property that has changed.
Such a variant of invoking an event is not the only one; on the Internet and in books one can find another couple of options. All this is a matter of taste. Personally, I love the direct challenge of the event, which is called head-on, I will show with an example, maybe it will become even clearer to you.
public class MainViewModel : INotifyPropertyChanged //не забываем о наследовании
We assign an empty delegate in the announcement of the event, so that later we don’t do the checking, and if there are any signatories to the event? They now always have at least one empty signer.
public event PropertyChangedEventHandler PropertyChanged = delegate { };
And here is an example of a property with a call to this event (using the new nameof() construction to pass the property name)
/// <summary> /// Имя скачиваемого видео файла /// </summary> private string _NameFileVideo; public string NameFileVideo { get { return _NameFileVideo; } set { if (_NameFileVideo == value) return; _NameFileVideo = value; PropertyChanged(this, new PropertyChangedEventArgs(nameof(NameFileVideo))); } }