大家好,我是XAML MVVM概念的新手,我知道一些CollectionViewSource的概念,我有一个带有GridView的WinRT应用程序,我想按行业将每个项目分组,我创建了一个ModelView如何转换为collectionView源,我在Google中搜索过但没有解决方案包含一些代码:

  <Page.Resources>
            <DataTemplate x:Key="MyDataTemplate">
                <Border  >

                    <Grid Background="DodgerBlue" Height="150" Width="200" Margin="0,-7,-7,0" RenderTransformOrigin="0.5,0.5" UseLayoutRounding="False">
                        <StackPanel VerticalAlignment="Bottom">
                            <StackPanel.Background>
                                <SolidColorBrush Color="Black" Opacity=".25"/>
                            </StackPanel.Background>
                            <TextBlock Text="{Binding Name}"/>
                            <TextBlock Text="{Binding Profession}"/>
                            <TextBlock Text="{Binding Age}"/>
                        </StackPanel>
                    </Grid>
                </Border>
            </DataTemplate>
            <ItemsPanelTemplate x:Key="ItemsPanelTemplate1">
                <VariableSizedWrapGrid x:Name="gridviewVariableSized" MaximumRowsOrColumns="4" Orientation="Vertical" />
            </ItemsPanelTemplate>

        </Page.Resources>
 <Page.DataContext>
        <ModelView:PeopleCollection/>
    </Page.DataContext>

最佳答案

要获取CollectionViewSource,请在ViewModel调用中

var groupable = CollectionViewSource.GetDefaultView(this.PeopleCollection);

(假设Peoplecollection是一个ObservableCollection或类似的对象。一旦有了这个对象,就可以使用
groupable.GroupDescriptions.Add(new PropertyGroupDescription("Country"));

关于c# - 创建CollectionViewSource,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14954214/

10-10 21:55