-
Couldn't load subscription status.
- Fork 0
MVVM PropertyChanged
Jan M edited this page Aug 12, 2020
·
2 revisions
The BindingBase is our default INotifyPropertyChanged implementation, both our ModelBase and ViewModelBase rely on it.
Features:
- implementation of
INofityPropertyChanged - convenient methods
GetValueandSetValueto easily declare a property and make use ofINotifyPropertyChanged -
INotifyPropertyChangedfor nested classes -
INotifyPropertyChangedfor dependent properties (e.g. update message forAgeproperty whenDateOfBirthhas changed) - controlling the
INotifyPropertyChangedevents via attribute - automatically updating
ICommand.CanExecuteif there is anyICommandproperty in the class (see: Options)
public class Model :
BindingBase
{
// simple property with PropertyChanged support
public string Name
{
get => return GetValue<string>();
set => SetValue(value);
}
// property with default value
public Guid ID
{
get => return GetValue(Guid.NewGuid());
set => SetValue(value);
}
// add custom PropertyChanged handler
public DateTime DateOfBirth
{
get => return GetValue<DateTime>();
set => SetValue(value, OnDateOfBirthChanged);
}
private void OnDateOfBirthChanged (object sender, PropertyChangedEventArgs eventArgs) { ... }
// add custom PropertyChanged handler
// and custom PropertyChanging handler
public int ZipCode
{
get => return GetValue<int >();
set => SetValue(value, OnZipCodeChanged, OnZipCodeChanging);
}
private void OnZipCodeChanged(object sender, PropertyChangedEventArgs eventArgs) { ... }
private void OnZipCodeChanging(object sender, PropertyChangingEventArgs eventArgs) { ... }
// add custom PropertyChanged action
public string Url
{
get => return GetValue<string>();
set => SetValue(value, (url) =>
{
if (!url.EndsWith('/'))
url += "/";
});
}
}Toggles whether ICommand.CanExecuteChanged should be invoked automatically on every PropertyChanged (false) event or only when a specific property has changed (true).
Default value is true.