Windows应用商店应用 - 所选项目的Gridview背景颜色

时间:2013-12-19 21:00:15

标签: gridview windows-store-apps visualstatemanager

我一直在谷歌搜索我的问题的答案,但没有一个论坛能够提供答案。我只想在选择后更改gridview中项目的背景颜色。我在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>

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

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

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:1)

找到Mike Taulty here的博客可能有所帮助。您可以使用Blend更改所有“无法访问”的样式。如果你看一下XAML Blend生成它实际上更有意义。