View Models:

public class Notes : BaseViewModel { public string Header { get; set; } public ObservableCollection<string> NoteLines { get; } } public class SomePage : BaseViewModel { public Notes Notes { get; set; } } 

Datatemplate:

  <DataTemplate DataType="local:Notes"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <TextBlock Grid.Row="0" Text="{Binding Header}"/> <ItemsControl Grid.Row="1" ItemsSource="{Binding NoteLines}"/> </Grid> </DataTemplate> <DataTemplate DataType="local:SomePage"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <SomeElement Grid.Column="0"/> <SomeElement Grid.Column="1"/> <!-- тут какой то способ отобразить Notes --> </Grid> </DataTemplate> 

I do not understand how to display the Notes property and how, for example, to display an instance of SomePage .

Plus, not sure, but how will the DataContext be transmitted?

    1 answer 1

    Good evening! Use MarkupExtension x:Type to specify the type of DataTemplate :

     <DataTemplate DataType="{x:Type local:SomePage}"> 

    To get the DataTemplate from the Nodes class up, use ContentControl . That is, instead of your comment write the following:

     <ContentControl Content="{Binding Nodes}" /> 

    DataTemplate will automatically pick up.

    From the binding, it can be noted that in the datacontext of these templates the view models themselves are, that is:

    For <DataTemplate DataType="local:SomePage"> DataContext = SomePage

    For <DataTemplate DataType="local:Notes"> DataContext = Notes

    By your example, I got about the following: enter image description here

    • x:Type not obvious at all, thanks for the help. - Monk