-
-
Notifications
You must be signed in to change notification settings - Fork 4
π View Models
Every ViewModel needs to implement the ViewModel
class:
class LoginViewModel : ViewModel
{
...
}
The ViewModel
base class inherits from ObservableObject
(see: View Models or Observable Objects π).
Using the ObservableObject
base class Set
function allows for quick notifying properties:
class LoginViewModel : ViewModel
{
private User _user;
public User User
{
get => _user;
set => Set(ref _user, value);
}
}
If you are using ReSharper you can define a notify-property-changed-property template π.
You can also force a PropertyChanged
invocation by using the Notify
function:
public string UserString => $"Mr./Mrs. {User}";
public User User
{
get => _user;
set
{
Set(ref _user, value);
Notify(nameof(UserString));
}
}
See also: View Model Locator π
You can bind your ViewModel to a given View by defining a DataTemplate in a resource dictionary (e.g. App.xaml
):
<DataTemplate DataType="{x:Type login:LoginViewModel}">
<login:LoginView />
</DataTemplate>
"To present the type
LoginViewModel
, show aLoginView
and set theDataContext
"
WPF will automatically look up the bound view to the data template if you set the Content
of any control to your view model:
<Window ...>
<UserControl Content="{Binding CenterContent}" />
</Window>
class MainViewModel : ViewModel
{
private object _centerContent;
public object CenterContent
{
get => _centerContent;
set => Set(ref _centerContent, value);
}
private void ShowLoginPage()
{
// Set content of UserControl to a ViewModel
CenterContent = new LoginViewModel();
}
}
As you can see, no UI code is required as WPF will automatically notice that the CenterContent
changed, and look up the corresponding View.