【发布时间】:2020-09-26 16:37:35
【问题描述】:
我在使用 UWP 时遇到了一个新问题。这一次我尝试显示一个包含不同项目的 AdaptiveGrid,所以我为每个项目设置了一个 DataTemplate 选择器,我要做的最后一件事是:根据它的类更改每个 GridViewItem 的 ItemHeight。 DataTemplateSelector 就像一个魅力......因此我创建了一个StyleSelector:
public class CardStyleTemplateSelector : StyleSelector
{
public Style CategoryStyle { get; set; }
public Style ReceipeStyle { get; set; }
public new Style SelectStyle(object item, DependencyObject container)
{
switch (item)
{
case Category category:
return CategoryStyle;
case Receipe receipe:
return ReceipeStyle;
}
return null;
}
}
很简单...所以我添加了一些样式:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="Category" TargetType="controls:AdaptiveGridView">
<Setter Property="ItemHeight" Value="240"/>
<Setter Property="Background" Value="Azure" />
</Style>
<Style x:Key="Receipe" TargetType="controls:AdaptiveGridView">
<Setter Property="ItemHeight" Value="400"/>
</Style>
</ResourceDictionary>
并在我的页面上使用此 XAML 完成:
<controls:AdaptiveGridView
Padding="{StaticResource MediumLeftRightMargin}"
animations:Connected.ListItemElementName="itemThumbnail"
animations:Connected.ListItemKey="animationKeyCategoryOverview"
DesiredWidth="220"
Margin="0,50,0,0"
IsItemClickEnabled="True"
ItemClick="OnItemClick"
ItemsSource="{x:Bind Source,Mode=OneWay}"
SelectionMode="None"
StretchContentForSingleRow="False"
ItemTemplateSelector="{StaticResource TemplateSelector}"
>
<controls:AdaptiveGridView.ItemContainerStyleSelector>
<templateSelectors:CardStyleTemplateSelector ReceipeStyle="{}" CategoryStyle="{StaticResource Category}" />
</controls:AdaptiveGridView.ItemContainerStyleSelector>
最后......我的背景没有改变......所以,也许你们中的一个可以指出我正确的方向......
【问题讨论】: