2016-12-15 2 views
-1

У меня есть элементы управления с источником предметов, и я хочу его показать превьюBind содержание ведущий контент для визуального элемента

<ItemsControl ItemsSource="{Binding Path=Children}"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate>          
      <ContentControl> 
       <ContentControl.ContentTemplate> 
        <DataTemplate> 
         <ContentPresenter Content="{Binding Preview}"/> 
        </DataTemplate> 
       </ContentControl.ContentTemplate> 
      </ContentControl> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

Просмотр сетки

public class Child 
{ 
    public Grid Preview { get; set; } 
    public Child() 
    { 
     Preview = new Grid(); 
     Preview.Children.Add(new TextBlock() { Text = "Test"}); 
     Preview.Background = new SolidColorBrush(Colors.Red); 
    } 
}  

Но это не Кажется, что-то делает, что мне не хватает?

+0

Создание элементов пользовательского интерфейса в вашей модели просмотра не является MVVM. – Will

ответ

0

попробовать, как это,

<ItemsControl ItemsSource="{Binding Children}"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <ContentPresenter Content="{Binding Preview}"/> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 

или

<ItemsControl ItemsSource="{Binding Path=Children}"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <ContentControl Content="{Binding}"> 
        <ContentControl.ContentTemplate> 
         <DataTemplate> 
          <ContentPresenter Content="{Binding Preview}"/> 
         </DataTemplate> 
        </ContentControl.ContentTemplate> 
       </ContentControl> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 

нет DataContext, поэтому оно не показывать ничего.