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
ContentTemplateproperty. Every control also has a
DataTemplatescollection 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
DataTemplatescollection 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.axamlfor a matching
DataTemplateand 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
DataTypeproperty on the template.
DataTemplatescollection should have its
DataTypeset to the type of the object that it matches, otherwise the data template won't match anything!
DataTemplatescollection the previous example could be written as:
DataTemplateeverywhere in a
Windowyou can specify it in
Window.DataTemplates; if you want the template to be used throughout the whole application you can specify it in
Teachertype and depending on the type of object in the
MainWindowViewModel.Contentproperty, the appropriate view will be displayed: