|
| 1 | +# How ViewModels are Turned into Views |
| 2 | + |
| 3 | +We make use of the [MVVM pattern](https://msdn.microsoft.com/en-us/library/ff798384.aspx), in which application level code is not aware of the view level. MVVM takes advantage of the fact that `DataTemplate`s can be used to create views from view models. |
| 4 | + |
| 5 | +## DataTemplates |
| 6 | + |
| 7 | +[`DataTemplate`](https://docs.microsoft.com/en-us/dotnet/framework/wpf/data/data-templating-overview)s are a WPF feature that allow you to define the presentation of your data. Consider a simple view model: |
| 8 | + |
| 9 | +```csharp |
| 10 | +public class ViewModel |
| 11 | +{ |
| 12 | + public string Greeting => "Hello World!"; |
| 13 | +} |
| 14 | +``` |
| 15 | + |
| 16 | +And a window: |
| 17 | + |
| 18 | +```csharp |
| 19 | +public class MainWindow : Window |
| 20 | +{ |
| 21 | + public MainWindow() |
| 22 | + { |
| 23 | + DataContext = new ViewModel(); |
| 24 | + InitializeComponent(); |
| 25 | + } |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +```xml |
| 30 | +<Window x:Class="MyApp.MainWindow" |
| 31 | + xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" |
| 32 | + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
| 33 | + xmlns:local="clr-namespace:MyApp" |
| 34 | + Content="{Binding}"> |
| 35 | +</pfui:DialogWindow> |
| 36 | + |
| 37 | +``` |
| 38 | + |
| 39 | +Here we're binding the `Content` of the `Window` to the `Window.DataContext`, which we're setting in the constructor to be an instance of `ViewModel`. |
| 40 | + |
| 41 | +One can choose to display the `ViewModel` instance in any way we want by using a `DataTemplate`: |
| 42 | + |
| 43 | +```xml |
| 44 | +<Window x:Class="MyApp.MainWindow" |
| 45 | + xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" |
| 46 | + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
| 47 | + xmlns:local="clr-namespace:MyApp" |
| 48 | + Content="{Binding}"> |
| 49 | + <Window.Resources> |
| 50 | + <DataTemplate DataType="{x:Type local:ViewModel}"> |
| 51 | + |
| 52 | + <!-- Display ViewModel.Greeting in a red border with rounded corners --> |
| 53 | + <Border Background="Red" CornerRadius="8"> |
| 54 | + <TextBlock Binding="{Binding Greeting}" |
| 55 | + </Border> |
| 56 | + |
| 57 | + </DataTemplate> |
| 58 | + </Window.Resources> |
| 59 | +</pfui:DialogWindow> |
| 60 | +``` |
| 61 | + |
| 62 | +This is the basis for converting view models to views. |
| 63 | + |
| 64 | +## ViewLocator |
| 65 | + |
| 66 | +There are currently two top-level controls for our UI: |
| 67 | + |
| 68 | +- [GitHubDialogWindow](../src/GitHub.VisualStudio/Views/Dialog/GitHubDialogWindow.xaml) for the dialog which shows the login, clone, etc views |
| 69 | +- [GitHubPaneView](../src/GitHub.VisualStudio/Views/GitHubPane/GitHubPaneView.xaml) for the GitHub pane |
| 70 | + |
| 71 | +In the resources for each of these top-level controls we define a `DataTemplate` like so: |
| 72 | + |
| 73 | +```xml |
| 74 | +<views:ViewLocator x:Key="viewLocator"/> |
| 75 | +<DataTemplate DataType="{x:Type vm:ViewModelBase}"> |
| 76 | + <ContentControl Content="{Binding Converter={StaticResource viewLocator}}"/> |
| 77 | +</DataTemplate> |
| 78 | +``` |
| 79 | + |
| 80 | +The `DataTemplate.DataType` here applies the template to all classes inherited from [`GitHub.ViewModels.ViewModelBase`](../src/GitHub.Exports.Reactive/ViewModels/ViewModelBase.cs) [1]. The template defines a single `ContentControl` whose contents are created by a `ViewLocator`. |
| 81 | + |
| 82 | +The [`ViewLocator`](../src/GitHub.VisualStudio/Views/ViewLocator.cs) class is an `IValueConverter` which then creates an instance of the appropriate view for the view model using MEF. |
| 83 | + |
| 84 | +And thus a view model becomes a view. |
| 85 | + |
| 86 | +[1]: it would be nice to make it apply to all classes that inherit `IViewModel` but unfortunately WPF's `DataTemplate`s don't work with interfaces. |
0 commit comments