Different item size in a grouped gridView

Viewed 8350

I created a Grouped GridView with an item template and header template. It works well but then I would like the first article of my list to have a different template (to be bigger). Like the french application "LeMonde" for example. I have no idea of how I could define the template to achieve that.

Here is my current xaml code

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

<Grid Background="White">

    <GridView  x:Name="PicturesGridView" SelectionMode="None"
ItemsSource="{Binding Source={StaticResource cvs1}}"IsItemClickEnabled="True"  ItemClick="ItemView_ItemClick">
        <GridView.ItemTemplate>
            <DataTemplate>
                <StackPanel x:Name="RectanglesStackPanel" Margin="8" Orientation="Vertical" Width="242">

                    <Image Source="{Binding imageUrl}" Height="180"  Width="225"   Stretch="UniformToFill" />

                    <Border Background="Gray" Opacity="1" Width="225" Height="115">
                       <TextBlock Text="{Binding title}"
           Foreground="White" TextWrapping="Wrap" Width="215" FontSize="18" />
                    </Border>
                </StackPanel>
            </DataTemplate>
        </GridView.ItemTemplate>
        <GridView.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </GridView.ItemsPanel>
        <GridView.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                         <Button  Click="Button_Click_1" Content="{Binding Key}" Foreground="Black" Background="White" FontSize="30" Margin="0,0,0,-10" ></Button>     
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
                <GroupStyle.Panel>
                    <ItemsPanelTemplate>
                        <VariableSizedWrapGrid Orientation="Vertical" />
                    </ItemsPanelTemplate>
                </GroupStyle.Panel>
            </GroupStyle>
        </GridView.GroupStyle>
    </GridView>


</Grid>

I just bind my list of item in C# like this :

this.cvs1.Source = ListOfArticle;

Thank you in advance :)


Thank you Ivan. It helps me a lot. However the code provided in the example doesn't seem up to date for Windows 8. Do you have any ideas of how we can access a template defined in the ressources of a page. The FindRessources Method doesn't exist anymore. I tried with this code instead but it wasn't successful :

 public class AuctionItemDataTemplateSelector : DataTemplateSelector
 {
    protected override DataTemplate SelectTemplateCore(object item, 
                                                  DependencyObject container)
    {
        FrameworkElement element = container as FrameworkElement;

        if (element != null && item != null && item is Article)
        {
            Article auctionItem = item as Article;

            DataTemplate mySmallTemplate = element.FindName("SmallTemplate") as DataTemplate;

            switch (auctionItem.isFirstItem)
            {
                case true:
                    return
                  element.FindName("BigTemplate") as DataTemplate;

                case false:
                    return
                   element.FindName("SmallTemplate") as DataTemplate;
            }
        }

        return null;
    }
}

'

3 Answers
Related