ListBox分组和LINQ

时间:2015-01-29 19:24:23

标签: c# wpf linq listbox

我可以在ListBox中使用LINQ进行分组吗? 我有财产

public IEnumerable<IGrouping<string, Rout>> GroupedTypeRouts
    {
        get { return Routs.GroupBy(x => x.Transport); }
    }

我希望在ListBox中按组显示它。

更新。 我试过这个

private CollectionViewSource groupedTypeRoutsView;
    public CollectionViewSource GroupedTypeRoutsView
    {
        get
        {
            if (groupedTypeRoutsView == null)
            {
                groupedTypeRoutsView = new CollectionViewSource();
                groupedTypeRoutsView.IsSourceGrouped = true;
                groupedTypeRoutsView.Source = GroupedTypeRouts;

            }

            return groupedTypeRoutsView;
        }
    }
public IEnumerable<IGrouping<string, Rout>> GroupedTypeRouts
    {
        get { return Routs.GroupBy(x => x.Transport); }
    }

但我不知道如何在ListBox中显示它

 <ListView Grid.Row="2"
                      Grid.ColumnSpan="9"
                      ItemsSource="{Binding Source=GroupedTypeRoutsView}">
                <ListView.GroupStyle>
                    <GroupStyle>
                        <GroupStyle.HeaderTemplate>
                            <DataTemplate>
                                <Image Source="{Binding Key, Converter={StaticResource TransportToImageConverter}}"></Image>
                            </DataTemplate>
                        </GroupStyle.HeaderTemplate>
                    </GroupStyle>
                </ListView.GroupStyle>
            </ListView>

我得到了GroupedTypeRoutsView而不是值和组

1 个答案:

答案 0 :(得分:0)

尝试:

    private ListCollectionView groupedTypeRoutsView;
    public ListCollectionView GroupedTypeRoutsView
    {
        get
        {
            if (groupedTypeRoutsView == null)
            {
                groupedTypeRoutsView = new ListCollectionView((IList)Routs);
                groupedTypeRoutsView.GroupDescriptions.Add(new PropertyGroupDescription("Transport"));
            }

            return groupedTypeRoutsView;
        }
    }


   <ListBox ItemsSource="{Binding GroupedTypeRoutsView}" />