如何在xaml中同时对列表框进行分组和排序?

时间:2017-05-29 11:21:11

标签: sorting xaml listbox uwp grouping

我想为listbox itemssource同时进行排序和分组。 我需要这样的东西Grouping and Sorting

我现在正在尝试这个:

<CollectionViewSource x:Key="cvs" x:Name="cvs" IsSourceGrouped="True"> </CollectionViewSource>

<ListBox Background="#282828" x:Name="sendersearchresults" ItemsSource="{Binding Source={StaticResource cvs}}">
                            <ListBox.GroupStyle>
                                <GroupStyle>
                                    <GroupStyle.HeaderTemplate>
                                        <DataTemplate>
                                            <StackPanel Margin="20,0,0,0">
                                                <TextBlock Foreground="#FFCF06" FontSize="40" FontWeight="Normal" Text="{Binding FirstName,Converter={StaticResource PrefixValueConverter},ConverterParameter=1}" />
                                                <Button Background="Transparent" Margin="-100,0,0,0" BorderThickness="0" BorderBrush="Transparent" Width="400">
                                                    <Grid>
                                                        <Grid.RowDefinitions>
                                                            <RowDefinition/>
                                                            <RowDefinition/>
                                                        </Grid.RowDefinitions>
                                                        <Image Source="{Binding Icon}" Grid.Row="0" Width="50" Height="50" HorizontalAlignment="Left" Margin="-12,4,0,0"/>
                                                        <TextBlock FontWeight="Medium" Text="{Binding MiddleName}" Margin="40,0,0,0" HorizontalAlignment="Left" Grid.Row="0" FontSize="20" Foreground="White"/>
                                                        <Grid Grid.Row="1">
                                                            <Grid.ColumnDefinitions>
                                                                <ColumnDefinition/>
                                                                <ColumnDefinition/>
                                                            </Grid.ColumnDefinitions>
                                                            <TextBlock FontWeight="Medium" Text="{Binding FirstName}" TextAlignment="Left" Margin="40,-22,0,0" Grid.Column="0" FontSize="20" Foreground="Gray"/>
                                                            <TextBlock FontWeight="Medium" HorizontalAlignment="Right" Grid.Column="0" Text="," FontSize="20" Margin="0,-22,-3,0" Foreground="Gray"/>
                                                            <TextBlock FontWeight="Medium" Text="{Binding LastName}" TextAlignment="Right" Grid.Column="1" Margin="6,-22,0,0" FontSize="20" Foreground="Gray"/>
                                                        </Grid>
                                                    </Grid>
                                                    <Button.Flyout>
                                                        <MenuFlyout Placement="Right">
                                                            <MenuFlyoutItem Text="Open" Click="MenuFlyoutItem_Click"/>
                                                            <MenuFlyoutItem Text="Edit" x:Name="editsenderclick" Click="editsenderclick_Click"/>
                                                        </MenuFlyout>
                                                    </Button.Flyout>
                                                </Button>
                                            </StackPanel>

                                        </DataTemplate>
                                    </GroupStyle.HeaderTemplate>
                                </GroupStyle>
                            </ListBox.GroupStyle>
                        </ListBox>

和c#:

listSearchResults.GroupBy(x => x.FirstName[0]).ToList(); listSearchResults.Sort((x,y)=> string.Compare(x.FirstName,y.FirstName)); cvs.Source = listSearchResults;

我得到了这个 result

提前致谢。

1 个答案:

答案 0 :(得分:0)

至少,我可以建议你将集合收集为代码:

cvs.Source = peopleCollection.OrderBy(x => x.FirstName).GroupBy(x => x.FirstName[0]).ToList();

之后,您将检索对象List<IGrouping<char, Person>>类型。

您在定义ListBox.GroupStyle时的错误。您必须在此部分中仅定义标题演示文稿,并在部分内为集合定义ItemTemplate。所以你需要这样的单独代码:

    <ListBox Background="#282828" x:Name="sendersearchresults" ItemsSource="{Binding Source={StaticResource cvs}}">
        <ListBox.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <StackPanel Margin="20,0,0,0">
                            <TextBlock Foreground="#FFCF06"
                                       FontSize="40"
                                       FontWeight="Normal" 
                                       Text="{Binding Key}" />
                        </StackPanel>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </ListBox.GroupStyle>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Button Background="Transparent" Margin="-100,0,0,0" BorderThickness="0" BorderBrush="Transparent" Width="400">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Image Source="{Binding Icon}" Grid.Row="0" Width="50" Height="50" HorizontalAlignment="Left" Margin="-12,4,0,0"/>
                        <TextBlock FontWeight="Medium" Text="{Binding MiddleName}" Margin="40,0,0,0" HorizontalAlignment="Left" Grid.Row="0" FontSize="20" Foreground="White"/>
                        <Grid Grid.Row="1">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <TextBlock FontWeight="Medium" Text="{Binding FirstName}" TextAlignment="Left" Margin="40,-22,0,0" Grid.Column="0" FontSize="20" Foreground="Gray"/>
                            <TextBlock FontWeight="Medium" HorizontalAlignment="Right" Grid.Column="0" Text="," FontSize="20" Margin="0,-22,-3,0" Foreground="Gray"/>
                            <TextBlock FontWeight="Medium" Text="{Binding LastName}" TextAlignment="Right" Grid.Column="1" Margin="6,-22,0,0" FontSize="20" Foreground="Gray"/>
                        </Grid>
                    </Grid>
                    <Button.Flyout>
                        <MenuFlyout Placement="Right">
                            <MenuFlyoutItem Text="Open" Click="MenuFlyoutItem_Click"/>
                            <MenuFlyoutItem Text="Edit" x:Name="editsenderclick" Click="editsenderclick_Click"/>
                        </MenuFlyout>
                    </Button.Flyout>
                </Button>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

查看最终结果(没有Icon和MiddleName)

[1]: https://i.stack.imgur.com/7A