C# UWP AdaptiveGridView ItemContainerStyleSelector don';行不通
我面临着一个新的UWP问题。这次我尝试显示一个包含不同项的AdaptiveGrid,因此我为每个项都有一个DataTemplate选择器,我最不想做的事情是:根据每个C# UWP AdaptiveGridView ItemContainerStyleSelector don';行不通,c#,gridview,uwp,C#,Gridview,Uwp,我面临着一个新的UWP问题。这次我尝试显示一个包含不同项的AdaptiveGrid,因此我为每个项都有一个DataTemplate选择器,我最不想做的事情是:根据每个GridViewItem的类更改其ItemHeight。DataTemplateSelector的工作方式很有魅力。。。。因此,我创建了一个样式选择器: public class CardStyleTemplateSelector : StyleSelector { public Style Categor
GridViewItem
的类更改其ItemHeight
。DataTemplateSelector的工作方式很有魅力。。。。因此,我创建了一个样式选择器
:
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>
最后。。。我的背景没有改变。。。所以,也许你们中的一位可以给我指出正确的方向…您的CardStyleTemplateSelector的实现不正确,您需要覆盖其SelectStyleCore以返回不同的样式,并且TargetType应该是GridViewItem而不是AdaptiveGridView。例如: .xaml:
<Style TargetType="GridViewItem" x:Key="Category">
<Setter Property="Background" Value="LightGray"/>
</Style>
<Style TargetType="GridViewItem" x:Key="Receipe">
<Setter Property="Background" Value="Red"/>
</Style>
......
<controls:AdaptiveGridView.ItemContainerStyleSelector>
<local:CardStyleTemplateSelector ReceipeStyle="{StaticResource Receipe}" CategoryStyle="{StaticResource Category}" />
</controls:AdaptiveGridView.ItemContainerStyleSelector>
不幸的是,这并没有起到作用:-/此外,ItemHeight属性是AdaptiveGrid的一部分,而不是任何GridViewItem的一部分。除此之外,我还找到了一个解决方法,可以绑定到保持宽度的局部变量……ItemContainerStyleSelector的作用域是针对每个GirdViewItem,如果您想更改ItemHeight属性,可以使用绑定。但是,即使您设置了不同的高度,每个项目最终也将应用相同的高度。我提供的上述方法是关于如何使用ItemContainerStyleSelector应用不同的样式。是的。那是我的遗嘱。目前,我只是在源代码中显示一个类的项。我的进一步目标是混合项目,这就是为什么我想使用样式选择器,不幸的是,它不起作用:-/所以当你尝试我的方法时,它不起作用?当我使用它时,它可以很好地改变背景。不幸的是,它不起作用。。。。也许你能给我完整的xaml,也许我把事情搞砸了
public class CardStyleTemplateSelector : StyleSelector
{
public Style CategoryStyle { get; set; }
public Style ReceipeStyle { get; set; }
protected override Style SelectStyleCore(object item, DependencyObject container)
{
switch (item)
{
case Category category:
return CategoryStyle;
case Receipe receipe:
return ReceipeStyle;
}
return null;
}
}