title | page_title | description | slug | tags | published | position |
---|---|---|---|---|---|---|
ViewModelBase |
ViewModelBase Class |
This article provides information about the ViewModelBase class. |
common-viewmodelbase-class |
ViewModelBase |
true |
5 |
The ViewModelBase
abstract class is designed to serve as a base class for all the model classes in your application. It provides support for property change notifications. Instead of implementing the INotifyPropertyChanged
interface in each individual view model, you can directly inherit ViewModelBase
.
{{region common-viewmodelbase-class-0}} using Telerik.Windows.Controls; public class MyViewModel : ViewModelBase { private object selectedItem; public object SelectedItem { get { return this.selectedItem; } set { if (value != this.selectedItem) { this.selectedItem = value; OnPropertyChanged(() => this.SelectedItem); } } }
private ObservableCollection<Club> clubsCollection;
public ObservableCollection<Club> ClubsCollection
{
get
{
return this.clubsCollection;
}
set
{
this.clubsCollection = value;
this.OnPropertyChanged("ClubsCollection");
}
}
}
{{endregion}}
As shown in the previous example the OnPropertyChanged
method gives a couple of overloads that can be used.
The ViewModelBase
class supports an additional approach for notifying the property change, via the the CallerMemberName attribute which is implemented in the RaisePropertyChanged
method.
{{region common-viewmodelbase-class-1}} public object SelectedItem { get { return this.selectedItem; } set { if (value != this.selectedItem) { this.selectedItem = value; RaisePropertyChanged(); } } } {{endregion}}
- [EventToCommandBehavior]({%slug common-event-to-command-behavior%})
- [Attached Behavior]({%slug common-mvvm-attached-behavior%})
- [DelegateCommand]({%slug common-mvvm-delegate-command-implementation%})