【问题标题】:UWP AdaptiveGridView ItemContainerStyleSelector don't workUWP AdaptiveGridView ItemContainerStyleSelector 不起作用
【发布时间】:2020-09-26 16:37:35
【问题描述】:

我在使用 UWP 时遇到了一个新问题。这一次我尝试显示一个包含不同项目的 AdaptiveGrid,所以我为每个项目设置了一个 DataTemplate 选择器,我要做的最后一件事是:根据它的类更改每个 GridViewItemItemHeight。 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>

最后......我的背景没有改变......所以,也许你们中的一个可以指出我正确的方向......

【问题讨论】:

    标签: c# gridview uwp


    【解决方案1】:

    您的 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>
    

    .cs:

    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;
        }
    }
    

    【讨论】:

    • 不幸的是,这并没有解决问题:-/除此之外,ItemHeight 属性是 AdaptiveGrid 的一部分,而不是任何 GridViewItem 的一部分。除此之外,我还有一个解决方法,可以绑定到保持宽度的局部变量...
    • ItemContainerStyleSelector的作用域是针对每个GirdViewItem,如果要改变ItemHeight属性,可以使用Binding。但是即使你设置不同,每个项目最终都会应用相同的高度我提供的上述方法是关于如何使用 ItemContainerStyleSelector 应用不同的样式。
    • 是的。这是我的意图。目前我只在源代码中显示 ONE 类的项目。我的进一步目标是混合项目,这就是我想使用 StyleSelector 的原因,不幸的是,它不起作用:-/
    • 那么当你尝试我的方法时,它没有奏效?我用的时候,它可以很好地改变背景。
    • 不幸的是它没有用....你可以给我你的完整xaml,也许我搞砸了
    猜你喜欢
    • 1970-01-01
    • 2017-11-04
    • 2017-11-08
    • 2020-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多