1

Вьюмодели:

  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>

Не понимаю, как отобразить свойство Notes и как потом например экземпляр SomePage отобразить.

Плюс, не уверен, а как будет передаваться DataContext?

1 ответ 1

2

Добрый вечер! Используйте MarkupExtension x:Type для указания типа DataTemplate:

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

Для того, чтобы DataTemplate от класса Nodes подхватился, используйте ContentControl. То есть, вместо вашего комментария напишите следующее:

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

DataTemplate автоматически подхватится.

Из байндингов можно заметить, что в датаконтексте этих темплейтов лежат сами вьюмодели, то есть:

Для <DataTemplate DataType="local:SomePage"> DataContext = SomePage

Для <DataTemplate DataType="local:Notes"> DataContext = Notes

По вашему примеру у меня получилось примерно следующее: введите сюда описание изображения

1
  • x:Type неочевиден совсем, спасибо за помощь.
    – Monk
    5 авг 2016 в 17:49

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.