GridView's ItemContainerStyle and selection states

Posted by Roberto Casadei on Stack Overflow See other posts from Stack Overflow or by Roberto Casadei
Published on 2014-05-28T07:30:37Z Indexed on 2014/05/29 9:27 UTC
Read the original article Hit count: 228

I'm trying to change the appearance of gridview items when they are selected. (Before, I used a trick with an IsSelected property in the ViewModel object bound to the containing grid and a bool-to-color converter, but I recognize that it is bad)

To do so, I do:

    <GridView ItemContainerStyle="{StaticResource GridViewItemContainerStyle}" ...> ...

and

    <Style x:Key="GridViewItemContainerStyle" TargetType="GridViewItem">
        <Setter Property="Background" Value="Red" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="GridViewItem">
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Grid.Background)" Storyboard.TargetName="itemGrid">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Black"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="SelectionStates">
                                <VisualState x:Name="UnselectedSwiping"/>
                                <VisualState x:Name="UnselectedPointerOver"/>
                                <VisualState x:Name="Selecting"/>
                                <VisualState x:Name="Selected">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Grid.Background)" Storyboard.TargetName="itemGrid">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="White"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="SelectedSwiping"/>
                                <VisualState x:Name="Unselecting"/>
                                <VisualState x:Name="Unselected"/>
                                <VisualState x:Name="SelectedUnfocused"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>

                        <Grid ... x:Name="itemGrid">
                              <!-- HERE MY DATA TEMPLATE -->
                        </Grid>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

When I run the app, the items are Black (as in the "normal" state). But selecting them does not turn them into White. Where am I wrong?

Moreover, it there a way to set "ItemContainerStyle" without having it to "overwrite" the "ItemTemplate" ???

© Stack Overflow or respective owner

Related posts about xaml

Related posts about windows-runtime