我用来在设计器d:DesignInstance中获取设计时数据。我的模型看起来像这样(简化):
interface IModel {
public ObservableCollection<IConversation> OpenConversations { get; }
}
enum ConversationType {
Channel, IM
}
interface IConversation {
public string Name { get; }
public ConversationType ConversationType { get; }
}
然后我有一个在OpenConversations属性中有几个条目的模拟模型。当用作ItemsSource我的ListView. 我的简化 XAML 视图如下所示:
<Page d:DataContext="{d:DesignInstance Type=mocks:MockModel, IsDesignTimeCreatable=True}">
<ListView ItemsSource="{Binding OpenConversations}"/>
</Page>
上面的示例按预期工作,我得到了设计时数据。
但是现在我想在我的ListView使用中添加分组,CollectionViewSource所以我在我的 XAML 中添加了以下内容:
<Page.Resources>
<CollectionViewSource x:Name="OpenConversations" IsSourceGrouped="True" />
</Page.Resources>
并将其更改ListView为:
<ListView ItemSource="{StaticResource OpenConversations}" />
我无法真正弄清楚的是如何将设计数据放入 中
CollectionViewSource,我尝试了以下方法,但它不起作用:
<CollectionViewSource ... Source="{Binding OpenConversations}" />
根据https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh780627.aspx上的文档,我需要(在代码隐藏中)分配CollectionViewSource.Source = from conversation in OpenConversations group by conversation.ConversationType into group select group。但我无法弄清楚如何使用设计时数据来做到这一点。