2016-12-15 83 views
-1

我有一個項目的項目源控制,並且我希望它顯示預覽綁定內容展示內容到一個可視元素

<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

在視圖模型中創建UI元素不是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的,所以它並沒有顯示任何內容。