2013-07-08 48 views
1

我有類似下面的對象模型:渲染集 - ItemsControl的

public class ViewModel 
{ 
public List<Group> Groups{ get; set; } 
} 

public class Group 
{ 
    public string Name { get; set; } 
    public List<Contact> Contacts { get; set; } 
} 

public class Contact 
{ 
    public string Name { get; set; } 
    public bool IsOnline { get; set; } 
} 

,我組結合到這樣一個ItemsControl:

<ItemsControl ItemsSource="{Binding Path=Groups}" 
     ItemTemplate="{StaticResource GroupTemplate}" > 
    </ItemsControl> 

和我的DataTemplate渲染他們。

 <DataTemplate x:Key="GroupTemplate" DataType="{x:Type Group}"> 
     </DataTemplate> 
<DataTemplate x:Key="ContactTemplate" DataType="{x:Type Contact}"> 
<StackPanel> 
<TextBlock Text="{Binding Name}"/> 
</StackPanle> 
     </DataTemplate> 

我怎麼能得到項目控件中顯示的聯繫人?聯繫人是每個組內的一個集合,我的viewmodel有一組組。讓它更復雜一點,我有不同的數據模板爲不同的聯繫人,我應該使用數據模板選擇器來選擇適當的聯繫人模板。另外請注意,我沒有任何可顯示的組模板,我只需要顯示聯繫人。

感謝, -Mike

+0

嗨,我有一種類似問題,但我我無法弄清楚。 –

回答

1

使用另一個ItemsControl的第一模板:

<DataTemplate x:Key="GroupTemplate" DataType="{x:Type my:Group}"> 
    <ItemsControl ItemsSource="{Binding Contacts}"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate DataType="{x:Type my:Contact}"> 
       <TextBlock Text="{Binding Name}"/> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 
</DataTemplate> 

並與模板選擇:

<DataTemplate x:Key="GroupTemplate" DataType="{x:Type my:Group}"> 
    <ItemsControl ItemsSource="{Binding Contacts}" 
       ItemTemplateSelector="{StaticResource yourContactItemSelector}"/> 
</DataTemplate> 
+0

嗨,我有類似的問題,但我無法弄清楚。 –

+0

相似問題:幫助將會被appriciated http://stackoverflow.com/questions/24445571/grid-view-or-listview-binding-or-rendering-of-collection-of-collection –