Windows应用商店应用:具有分组GridView的不均匀UI

时间:2013-07-16 14:03:20

标签: c# windows-store-apps winrt-xaml

首先,我对Windows 8和开发Windows商店应用程序非常陌生。

我正在尝试将GridView控件添加到我当前的应用程序中,但它看起来有些问题:

enter image description here

你可以看到我的小组不平衡。理想情况下,我希望所有团体看起来像第一组。 这是我一直遵循的教程:http://www.wintellect.com/blogs/sloscialo/windows-8-gridview-and-variable-sized-items在某些时候我想让每组的第一块比其余的更大。

以下是使用的代码:

.xaml文件:

    <GridView
        x:Name="itemGridView"
        Grid.RowSpan="2"
        Padding="120, 130, 0, 0"
        ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"
        ItemContainerStyleSelector="{StaticResource VariableSizedStyleSelector}"
        SelectionMode="None"
        IsSwipeEnabled="False"
        IsItemClickEnabled="True">

        <GridView.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <Grid Margin="5, 0, 0, 0">
                            <Button
                                AutomationProperties.Name="Group Title"
                                Style="{StaticResource TextPrimaryButtonStyle}" >
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="{Binding Title}" Margin="0, 0, 0, 0" Style="{StaticResource GroupHeaderTextStyle}" Opacity=".25" />
                                </StackPanel>
                            </Button>
                        </Grid>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
                <GroupStyle.Panel>
                    <ItemsPanelTemplate>
                        <VariableSizedWrapGrid Margin="0, 10, 50, 0" />
                    </ItemsPanelTemplate>
                </GroupStyle.Panel>
            </GroupStyle>
        </GridView.GroupStyle>
    </GridView>

和项目样式:

<Style x:Key="DefaultGridViewItemStyle"
   TargetType="GridViewItem">
    <Setter Property="VariableSizedWrapGrid.RowSpan" Value="4" />
    <Setter Property="VariableSizedWrapGrid.ColumnSpan" Value="2" />
    <Setter Property="Background" Value="#E5E5E5" />
</Style>

请帮忙!

由于

1 个答案:

答案 0 :(得分:1)

GridView分组可以使用 CollectionViewSource 完成。 在您的Xaml中,在页面的“资源”部分添加 CollectionViewSource 。确保将 IsSourceGrouped 设置为true:

<Page.Resources>
    <CollectionViewSource x:Name="FruitsCollectionViewSource" IsSourceGrouped="True"/>
</Page.Resources>

我们可以稍后从代码隐藏设置 CollectionViewSource 来源

然后 CollectionViewSource (FruitsCollectionViewSource)应设置为 GridView

ItemsSource
 <GridView
        ItemsSource="{Binding Source={StaticResource FruitsCollectionViewSource}}"
        x:Name="FruitGridView"
        Padding="330,20,40,0"
        SelectionMode="None"
        IsSwipeEnabled="false"
        IsItemClickEnabled="True"
        ItemClick="FruitGridView_ItemClick">

          //// GridView ItemTemplate

            <GridView.ItemTemplate>
                <DataTemplate>
                    <Grid HorizontalAlignment="Left" Width="250" Height="250">
                        <Border Background="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}">
                            <Image Source="{Binding Path=FruitImageSource}" Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}"/>
                        </Border>
                        <StackPanel VerticalAlignment="Bottom" Background="{ThemeResource ListViewItemOverlayBackgroundThemeBrush}">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="Fruit Name" Foreground="{ThemeResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource TitleTextBlockStyle}" Height="30" Margin="15,0,15,0"/>
                                <TextBlock Text="{Binding Path=FruitName}" Style="{StaticResource TitleTextBlockStyle}" Height="30" Margin="15,0,15,0"/>
                            </StackPanel>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="Fruit Rate" Foreground="{ThemeResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource CaptionTextBlockStyle}" TextWrapping="NoWrap" Margin="15,0,87,10"/>
                                <TextBlock Text="{Binding Path=FruitRate}" Foreground="{ThemeResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource CaptionTextBlockStyle}" TextWrapping="NoWrap" Margin="15,0,15,10"/>
                            </StackPanel>                          
                        </StackPanel>
                    </Grid>
                </DataTemplate>
            </GridView.ItemTemplate>

            ////Group Style

            <GridView.GroupStyle>
                <GroupStyle>
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <TextBlock Text='{Binding Key}' Foreground="Gray" Margin="5" FontSize="30" FontFamily="Segoe UI Light" />
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                    <GroupStyle.Panel>
                        <ItemsPanelTemplate>
                            <VariableSizedWrapGrid MaximumRowsOrColumns="2" Orientation="Horizontal" />
                        </ItemsPanelTemplate>
                    </GroupStyle.Panel>
                </GroupStyle>
            </GridView.GroupStyle>

             //// Item panel Style

            <GridView.ItemsPanel>
                <ItemsPanelTemplate>
                    <ItemsWrapGrid GroupPadding="0,0,70,0" />
                </ItemsPanelTemplate>
            </GridView.ItemsPanel>                     
        </GridView>

现在,您可以从代码隐藏

FruitList 添加项目

创建 ObservableCollection FruitList

public ObservableCollection<Fruit> FruitList { get; set; }

创建一个模型类Fruit

    public class Fruit
    {
        public string FruitName { get; set; }
        public string FruitImageSource { get; set; }
        public string FruitRate { get; set; }
    }

然后将项目添加到 FruitList

    FruitList.Add(new Fruit { FruitName = "Blackberry", FruitImageSource = "../Assets/blackberry.jpg", FruitRate = "150" });
    FruitList.Add(new Fruit { FruitName = "Apple", FruitImageSource = "../Assets/apple.jpg", FruitRate = "150" });
    FruitList.Add(new Fruit { FruitName = "Orange", FruitImageSource = "../Assets/orange.jpg", FruitRate = "250" });
    FruitList.Add(new Fruit { FruitName = "Bilberry", FruitImageSource = "../Assets/bilberry.jpg", FruitRate = "250" });
    FruitList.Add(new Fruit { FruitName = "Banana", FruitImageSource = "../Assets/banana.jpg", FruitRate = "50" });
    FruitList.Add(new Fruit { FruitName = "Amla", FruitImageSource = "../Assets/amla.jpg", FruitRate = "120" });

现在您可以设置 CollectionViewSource 的来源 首先,我们需要对 FruitList 进行排序。然后使用此 GetGroupsByLetter()方法按升序或降序对水果名称进行分组。

        FruitList = new ObservableCollection<Fruit>(FruitList .OrderBy(c => c.FruitName));
        FruitsCollectionViewSource.Source = GetGroupsByLetter();

GetGroupsByLetter() FruitList 中的项目进行分组。

    internal List<GroupInfoList<object>> GetGroupsByLetter()
    {
        var groups = new List<GroupInfoList<object>>();

        var query = from item in FruitList
                    orderby ((Fruit)item).FruitName
                    group item by ((Fruit)item).FruitName[0] into g
                    select new { GroupName = g.Key, Items = g };
        foreach (var g in query)
        {
            var info = new GroupInfoList<object>();
            info.Key = g.GroupName;
            foreach (var item in g.Items)
            {
                info.Add(item);
            }

            groups.Add(info);
        }

        return groups;
    }

GroupInfoList:

    public class GroupInfoList<T> : List<object>
    {
        public object Key { get; set; }

        public new IEnumerator<object> GetEnumerator()
        {
            return (System.Collections.Generic.IEnumerator<object>)base.GetEnumerator();
        }
    }

现在你已经完成了。

您的输出将如下所示

enter image description here