Window.Content
property to the MainWindowViewModel.List
property which is an instance of TodoListViewModel
but the window is displaying a TodoListView
! What's happening here? How is a view being displayed when the window content is a view model?ViewLocator.cs
which was a file added by the template:Match(object data)
looks at the data and if the data inherits from ViewModelBase
it returns true
indicating that Build
should be calledBuild(object data)
takes the fully qualified name of the data's type and replaces the string "ViewModel"
with the string "View"
. It then tries to get a type that matches that name. If a matching type is found, it creates an instance of the type and returns itViewLocator
is present in Application.DataTemplates
:Window
) has its Content
property set to a non-control, it searches up the tree of controls for a DataTemplate
that matches the content data. If no other DataTemplate
matches the data it will eventually reach the ViewLocator
in the application data templates which will do its business and return an instance of the corresponding view.ViewLocator
is included in the project's source instead of being a component of Avalonia itself because the mechanism of relating a view model to a view may be application-specific; for example one might want to implement it using a DI framework. ViewLocator
can be thought of as implementing the convention over configuration paradigm.