This is how we do it normally:
public class ViewModel : INotifyPropertyChanged
{
string _test;
public string Test
{
get { return _test; }
set
{
_test = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string property = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
Now our property can be used by multiple elements in the view, e.g.:
<TextBox Text="{Binding Test, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Text="{Binding Test}" />
Changing value in TextBox
will update content of TextBlock
. As well as we can set value in the view model and view will update it automatically.
If we write view model like this
public class ViewModel
{
public string Test { get; set; }
}
then the view is still working (e.g. changing value in TextBox
will update TextBlock
). Of course it's not possible to easily update Test
value from view model anymore (no more event to rise). But my question is about view: Why view is able to work? Does it construct something more in background or is it a logic which checks for something?
OnPropertyChanged
the view is still working (e.g. changing value in TextBox will update TextBlock) Of course, the view updates the Source (that's why you set up when inUpdateSourceTrigger
). But in the second case, if you change the value of Test in your viewmodel, the View won't be notifyed and you won't see that update in the view.. – Topsoil