Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
棘手的WPF样式问题-你能做到吗?_Wpf_Mvvm_Observablecollection_Itemscontrol_Styling - Fatal编程技术网

棘手的WPF样式问题-你能做到吗?

棘手的WPF样式问题-你能做到吗?,wpf,mvvm,observablecollection,itemscontrol,styling,Wpf,Mvvm,Observablecollection,Itemscontrol,Styling,我有一个可观察的项目集合,其中每个项目都有一个名称和一个“组名”(两个字符串) 棘手的部分是,在XAML中,我需要设置此集合的样式,以便具有相同组名的所有项彼此相邻列出,并且组名显示在顶部 我为组名的布局以及显示的每个项目的布局设计了用户控件。我使用过ItemsControl,但不知道如何设置它的样式,以便具有相同组的项以某种方式“合并”到使用通用ItemControl用户控件作为其源的相同ItemsControl中,组名显示在顶部 我确实通过使用“组”集合实现了这一点,每个组中都有一个项目集合

我有一个可观察的项目集合,其中每个项目都有一个名称和一个“组名”(两个字符串)

棘手的部分是,在XAML中,我需要设置此集合的样式,以便具有相同组名的所有项彼此相邻列出,并且组名显示在顶部

我为组名的布局以及显示的每个项目的布局设计了用户控件。我使用过ItemsControl,但不知道如何设置它的样式,以便具有相同组的项以某种方式“合并”到使用通用ItemControl用户控件作为其源的相同ItemsControl中,组名显示在顶部

我确实通过使用“组”集合实现了这一点,每个组中都有一个项目集合,因为绑定工作得非常好。但是,我没有考虑到每个项目可能在多个组中(将一个人视为在“工作朋友”和“同事”组中,因此需要复制)


非常感谢任何建议或解决方案:)

首先,使用
CollectionViewSource
对项目进行排序:

<CollectionViewSource  x:Key="SortedItems" Source="{Binding YourUnsortedItems}">
    <CollectionViewSource.SortDescriptions>
        <scm:SortDescription PropertyName="GroupName"/>
    </CollectionViewSource.SortDescriptions>
</CollectionViewSource>
<ItemsControl Source="{Binding Source={StaticResource SortedItems}}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <!-- stick your template for each item here -->
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>