1

标准 GridApp 模板如下: 在此处输入图像描述

Variable Size Grouped GridView 模板如下: 在此处输入图像描述

如何为您的应用程序制作模板,使其看起来像这样:

在此处输入图像描述

例如,此设计在所有应用程序 Bing for windows 8 中: 在此处输入图像描述

可变大小分组 GridView 模板的代码:

<UserControl.Resources>

    <!-- Collection of grouped items displayed by this page -->
    <CollectionViewSource
        x:Name="groupedItemsViewSource"
        Source="{Binding Groups}"
        IsSourceGrouped="true"
        ItemsPath="Items"
        d:Source="{Binding ItemGroups, Source={d:DesignInstance Type=data:SampleDataSource, IsDesignTimeCreatable=True}}"/>

    <DataTemplate x:Key="CustomTileItem">
        <Grid >
            <Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}">
                <Image Source="{Binding Image}" Stretch="UniformToFill"/>
            </Border>
            <StackPanel VerticalAlignment="Bottom" >
                <TextBlock Text="{Binding Title}" Foreground="{StaticResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource TitleTextStyle}" Height="30" Margin="15,0,15,0"/>
                <TextBlock Text="{Binding Subtitle}" Foreground="{StaticResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource CaptionTextStyle}" TextWrapping="NoWrap" Margin="15,0,15,10"/>
            </StackPanel>
        </Grid>
    </DataTemplate>
</UserControl.Resources>

<!--
    This grid acts as a root panel for the page that defines two rows:
    * Row 0 contains the back button and page title
    * Row 1 contains the rest of the page layout
-->
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition Height="140"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!-- Back button and page title -->
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Button x:Name="backButton" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyle}"/>
        <TextBlock x:Name="pageTitle" Text="{StaticResource AppName}" Grid.Column="1" Style="{StaticResource PageHeaderTextStyle}"/>
    </Grid>

    <!-- Horizontal scrolling grid used in most view states -->
    <ScrollViewer
        x:Name="itemGridScrollViewer"
        AutomationProperties.AutomationId="ItemGridScrollViewer"
        Grid.Row="1"
        Margin="0,-3,0,0"
        Style="{StaticResource HorizontalScrollViewerStyle}">

        <local:MyGridView
            x:Name="itemGridView"
            AutomationProperties.AutomationId="ItemGridView"
            AutomationProperties.Name="Grouped Items"
            Margin="116,0,40,46"
            ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"
            ItemTemplate="{StaticResource CustomTileItem}"
            SelectionMode="None"
            IsItemClickEnabled="True"
            ItemClick="ItemView_ItemClick">

            <local:MyGridView.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </local:MyGridView.ItemsPanel>
            <local:MyGridView.GroupStyle>
                <GroupStyle>
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <Grid Margin="1,0,0,6">
                                <Button
                                    AutomationProperties.Name="Group Title"
                                    Content="{Binding Title}"
                                    Click="Header_Click"
                                    Style="{StaticResource TextButtonStyle}"/>
                            </Grid>
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                    <GroupStyle.Panel>
                        <ItemsPanelTemplate>
                            <VariableSizedWrapGrid ItemWidth="75" ItemHeight="150" Orientation="Vertical" Margin="0,0,80,0" MaximumRowsOrColumns="3"/>
                        </ItemsPanelTemplate>
                    </GroupStyle.Panel>
                </GroupStyle>
            </local:MyGridView.GroupStyle>
        </local:MyGridView>
    </ScrollViewer>

和:

public class MyGridView : GridView
{
    private int rowVal;
    private int colVal;
    private Random _rand;
    private List<Size> _sequence;

    public MyGridView()
    {
        _rand = new Random();
        _sequence = new List<Size> { 
            LayoutSizes.PrimaryItem, 
            LayoutSizes.SecondarySmallItem, LayoutSizes.SecondarySmallItem, 
            LayoutSizes.SecondarySmallItem, 
            LayoutSizes.SecondaryTallItem, 
            LayoutSizes.OtherSmallItem, LayoutSizes.OtherSmallItem, LayoutSizes.OtherSmallItem
        };

    }
    protected override void PrepareContainerForItemOverride(Windows.UI.Xaml.DependencyObject element, object item)
    {
        base.PrepareContainerForItemOverride(element, item);
        SampleDataItem dataItem = item as SampleDataItem;
        int index = -1;

        if (dataItem != null)
        {
            index = dataItem.Group.Items.IndexOf(dataItem);

        }



        if (index >= 0 && index < _sequence.Count)
        {

            colVal = (int)_sequence[index].Width;
            rowVal = (int)_sequence[index].Height;

        }
        else
        {
            colVal = (int)LayoutSizes.OtherSmallItem.Width;
            rowVal = (int)LayoutSizes.OtherSmallItem.Height;

        }


        VariableSizedWrapGrid.SetRowSpan(element as UIElement, rowVal);
        VariableSizedWrapGrid.SetColumnSpan(element as UIElement, colVal);


    }
}

public static class LayoutSizes
{
    public static Size PrimaryItem = new Size(6, 2);
    public static Size SecondarySmallItem = new Size(3, 1);
    public static Size SecondaryTallItem = new Size(3, 2);
    public static Size OtherSmallItem = new Size(2, 1);

}

例如“Variable Sized Grouped GridView template”,我们可以组合行或列,以及如何设置第一个元素的height =“auto”,以及所有其他具有不同宽度和高度但分组为“Variable Sized Grouped”的元素GridView 模板”?

4

1 回答 1

1

查看这篇文章:http: //blogs.msdn.com/b/synergist/archive/2012/09/25/windows-store-app-xaml-gridview-with-variable-templates.aspx

我认为这就是你所需要的。

于 2013-04-24T20:48:47.960 回答