Content
property, such as ContentControl.Content
. Window
inherits from ContentControl
, so lets use that as an example. You're probably familiar with what happens when you put a control in the Window.Content
property - the window displays the control:Student
- because it's not a control it falls back to just calling .ToString()
on the object. We can tell Avalonia how to display non-control objects by defining a data template.Window
(and any control that inherits from ContentControl
) is to set the ContentTemplate
property:ContentTemplate
property. Every control also has a DataTemplates
collection into which any number of data templates can be placed. If a control doesn't have a template set locally (e.g. in ContentTemplate
) then it will look in its DataTemplates
collection for a matching template. If a match isn't found there it will then go on to search its parent's DataTemplates
, then its grandparent's, and so on until it reaches the Window
. If it still hasn't found a match it will then look in App.xaml
/App.axaml
for a matching DataTemplate
and finally when all those options have been exhausted it will simply call .ToString()
on the object.DataTemplate
s are matched by type: the type that the template matches is specified by setting the DataType
property on the template.Remember: EachDataTemplate
in theDataTemplates
collection should have itsDataType
set to the type of the object that it matches, otherwise the data template won't match anything!
DataTemplates
collection the previous example could be written as:DataTemplate
everywhere in a Window
you can specify it in Window.DataTemplates
; if you want the template to be used throughout the whole application you can specify it in App.xaml
/App.axaml
in the Application.DataTemplates
collection.Teacher
type and depending on the type of object in the MainWindowViewModel.Content
property, the appropriate view will be displayed: