【问题标题】:Windows Store App - Gridview background color for selected itemsWindows 应用商店应用程序 - 所选项目的 Gridview 背景颜色
【发布时间】:2014-04-02 20:44:37
【问题描述】:

我一直在谷歌上搜索我的问题的答案,但没有一个论坛能够提供答案。我只是想在选择网格视图后更改项目的背景颜色。我的 App.xaml 中有一个样式定义,我将它链接到我的 ItemContainerStyle,如下所示:

<GridView x:Name="gvwTests" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Top"
    Width="998" Height="567" Margin="10,51,0,0" Padding="5,0,5,0"
    Background="#FF768E9C"
    ItemTemplate="{StaticResource testTemplate}"
    Style="{StaticResource PAGridViewStyle}" ItemContainerStyle="{StaticResource PAGridViewItemStyle}"
    IsDoubleTapEnabled="False" IsRightTapEnabled="False" SelectionMode="Multiple" SelectionChanged="GvwTests_SelectionChanged">
</GridView>

我生成了默认样式的副本:

<Style x:Key="PAGridViewItemStyle" TargetType="GridViewItem">
    <Setter Property="Background" Value="#0077FF" />
    <Setter Property="Margin" Value="0 0 5 5"/>
    <Setter Property="Padding" Value="20 40 40 40" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="GridViewItem">
                <Border x:Name="OuterContainer">
                    ...
                    <VisualStateManager.VisualStateGroups>
                        ...
                        <VisualStateGroup x:Name="SelectionStates">
                            <VisualState x:Name="Selecting">
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="SelectionBackground"
                                                Storyboard.TargetProperty="Opacity"
                                                Duration="0"
                                                To="1" />
                                    <DoubleAnimation Storyboard.TargetName="SelectedBorder"
                                                Storyboard.TargetProperty="Opacity"
                                                Duration="0"
                                                To="1" />
                                    <DoubleAnimation Storyboard.TargetName="SelectingGlyph"
                                                Storyboard.TargetProperty="Opacity"
                                                Duration="0"
                                                To="1" />
                                    <DoubleAnimation Storyboard.TargetName="HintGlyphBorder"
                                                Storyboard.TargetProperty="Opacity"
                                                Duration="0"
                                                To="1" />
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="contentPresenter"
                                                            Storyboard.TargetProperty="Foreground">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ListViewItemSelectedForegroundThemeBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ColorAnimation Storyboard.TargetName="SelectionBackground"
                                                    Storyboard.TargetProperty="Color"
                                                    Duration="0:0:1"
                                                    From="Red" To="Beige" />
                                </Storyboard>
                            </VisualState>
                            ...
                        </VisualStateGroup>
                        ...
                    </VisualStateManager.VisualStateGroups>
                    ...
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我不是这方面的专家,所以当项目被点击或点击时,我无法改变颜色。

我应该怎么做或者我做错了什么?

【问题讨论】:

    标签: gridview windows-store-apps visualstatemanager


    【解决方案1】:

    背景颜色更容易在 App.xaml 中更改为覆盖:

    <Application.Resources>
      <ResourceDictionary>
        <SolidColorBrush x:Key="ListViewItemSelectedPointerOverBackgroundThemeBrush" Color="#56c2ff" />
        <SolidColorBrush x:Key="ListViewItemSelectedPointerOverBorderThemeBrush" Color="#56c2ff" />
        <SolidColorBrush x:Key="ListViewItemSelectedBackgroundThemeBrush" Color="#56c2ff" />
        ...
    

    【讨论】:

    • 如果我理解正确,您是在覆盖默认画笔吗?那是天才,我太愚蠢了!我之前尝试过类似的操作,但出现错误说资源字典需要 x 键:/ 我明天会尝试,我会回复你。谢谢!
    • 好的,我试过了,但我收到一个错误,提醒我资源字典需要一个键。我随机添加一个(x:Key="ListStyles")。知道为什么不应用这些样式吗?
    • 尝试删除您的样式。默认样式位于此处:C:\Program Files (x86)\Windows Kits\8.0\Include\WinRT\Xaml\Design\generic.xaml
    【解决方案2】:

    发现 Mike Taulty here 的博客可能会有所帮助。您使用 Blend 来更改所有“无法访问”的样式。如果您查看 XAML Blend 生成的内容,它实际上更有意义。

    【讨论】:

    • 嗨,感谢您的意见 :) 问题已解决,但我会记住博客,看起来不错。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-06
    • 2014-07-05
    • 2016-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多