Skip to content

πŸ“ View Models

mrousavy edited this page Aug 21, 2018 · 7 revisions

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 πŸ“–

Data Templates

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 a LoginView and set the DataContext"

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.

Clone this wiki locally