Many controls have a 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:
Not very helpful. That's because Avalonia doesn't know how to display an object of type 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.
The easiest way to do this on Window (and any control that inherits from ContentControl) is to set the ContentTemplate property:
The data template for the window content doesn't only come from the 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.
DataTemplates are matched by type: the type that the template matches is specified by setting the DataType property on the template.
Remember: Each DataTemplate in the DataTemplates collection should have its DataType set to the type of the object that it matches, otherwise the data template won't match anything!
Using the DataTemplates collection the previous example could be written as:
Using this mechanism, if you want to reuse a 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.
Now we can add a separate data template for the Teacher type and depending on the type of object in the MainWindowViewModel.Content property, the appropriate view will be displayed:
Data templates in Avalonia can target interfaces and derived classes and so the order of DataTemplates can be important: DataTemplates within the same collection are evaluated in declaration order so you need to place them from most-specific to least-specific as you would in code.