Using d:DesignInstance with CollectionViewSource

本文关键字:with CollectionViewSource DesignInstance Using | 更新日期: 2023-09-27 18:05:54

我使用d:DesignInstance来获取设计器中的设计时数据。我的模型是这样的(简化):

interface IModel {
  public ObservableCollection<IConversation> OpenConversations { get; }
}
enum ConversationType {
  Channel, IM
}
interface IConversation {
  public string Name { get; }
  public ConversationType ConversationType { get; }
}

然后我有一个模拟模型,在OpenConversations属性中有几个条目。当在我的ListView中用作ItemsSource时,这工作得很好。我的简化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。但是我不知道如何使用设计时数据来做到这一点。

Using d:DesignInstance with CollectionViewSource

首先试着定义一个组应该是什么样子我的意思是组的标题,还有更多

public class  Group<T> : ObservableCollection<T>, INotifyPropertyChanged
{
  public Group(string name,IEnumerable<T> items):base(items)
   { Name=name;
   }
  //more ...
}

这个类不仅仅用于静态数据,它处理不断变化的数据。现在,如果你使用MVVM模型,在你的视图模型(home)中定义一个属性ListGroup(名称你想要的)类型为new ObservableCollection>PS不是真正的代码,只是给出一个想法的模型。

 var s = from val in OpenConversations 
         group val by val.Conversationb into g 
         select new Group<IConversation>(g.Key.ToString(), g);
 GroupList = new ObservableCollection<Group<IConversation>>(s);

现在像之前一样在资源

中创建一个CollectionViewSource
    <CollectionViewSource x:Key="cvs" IsSourceGrouped="True" Source="{Binding Source={StaticResource home},Path=GroupList}"/>
      <ListView ItemsSource="{Binding Source={StaticResource cvs}}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Background="Salmon" Width="100" Height="100">
                    <TextBlock  Text="{Binding Name}"/>
                </StackPanel>
            </DataTemplate>              
        </ListView.ItemTemplate>
        <ListView.GroupStyle>
            <GroupStyle >
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <Grid Background="LightGray"  >
                            <TextBlock Text='{Binding Name}' Foreground="Black" Margin="10"
                           Style="{StaticResource SubheaderTextBlockStyle}" />
                        </Grid>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </ListView.GroupStyle>               
    </ListView>

看看如何定义GroupSyle你在这里添加更多的属性它最适合完全控制,我在w10应用程序中使用它。加上a有部分设计时间的帮助(只显示列表):))不显示组(GroupView)

的标题