Here’s another quickie
Question : Does your DataContext (or ViewModel if you’re doing MVVM) have to implement INotifyPropertyChanged in order to participate in XAML data binding and change synchronization (and all other goodies that follow)
Answer: Absolutely NOT !
Its an almost ubiquitous misunderstanding that unless you implement INotifyPropertyChanged on your class that is used as a binding source/target (DataContext) then your XAML wont work. Well it will – perfectly ! (well almost)
When you create a binding expression on XAML like this
<TextBox Text="{Binding Name}" Margin="5" Grid.Column="1" />
and your DataContext is a class like this (notice complete lack of INotifyPropertyChanged)
public class MyModel { public string Name { get; set; } }
what the XAML databinding subsystem does is in this example is add the ‘Name’ property of your DataContext class to a dictionary of values that it is tracking internally. Any time a binding expression changes this property, not only will the property setter be correctly set, but other usages of this property in your XAML will automatically get updated – which is 95% of the functionality that most implementations require.
The usual extras like value converter and update trigger work as you’d expect – e.g:
<TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" Margin="5" Grid.Column="1" /> <TextBlock Text="{Binding Name, Converter={StaticResource TestValueConverter}}" Margin="5" Grid.Column="2" />
all works as expected.
So what’s the 5% that doesn’t work
Well, it simply wont work if you change a property directly in your class, rather than through the XAML, which makes sense – as how would the XAML know if you’ve been changing properties behind its back.