【问题标题】:Rendering Collection of collections - ItemsControl呈现集合的集合 - ItemsControl
【发布时间】:2013-07-08 10:11:36
【问题描述】:

我有一个如下的对象模型:

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  ItemsSource="{Binding Path=Groups}"
       ItemTemplate="{StaticResource GroupTemplate}" >
    </ItemsControl>

我有渲染它们的数据模板。

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

如何在项目控件中显示联系人?联系人是每个组内的一个集合,我的视图模型有一个组集合。更复杂一点的是,我为不同的联系人提供了不同的数据模板,我应该使用数据模板选择器来选择合适的联系人模板。另请注意,我在群组模板中没有任何内容可显示,我只需要显示联系人即可。

谢谢, -迈克

【问题讨论】:

  • 嗨,我有类似的问题,但我无法弄清楚。

标签: wpf


【解决方案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>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多