Я создал Grouped GridView с шаблоном элемента и шаблоном заголовка. Это работает хорошо, но тогда я хотел бы, чтобы первая статья моего списка имела другой шаблон (, чтобы он был больше ). Как, например, французское приложение «LeMonde». Я понятия не имею, как я могу определить шаблон для достижения этого.
Вот мой текущий код xaml
<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>
Я просто связываю свой список элементов в C #вот так:
this.cvs1.Source = ListOfArticle;
Заранее спасибо:)
Спасибо, Иван. Это мне очень помогает. Однако код, представленный в примере, кажется неактуальным для Windows 8. Есть ли у вас какие-либо идеи о том, как мы можем получить доступ к шаблону, определенному в ресурсах страницы. Метод FindRessources больше не существует. Вместо этого я попытался использовать этот код, но он не увенчался успехом:
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;
}
}