为什么我的ListView-Item只跳回VisualState,一次?

时间:2016-05-30 08:49:35

标签: c# visual-studio xaml uwp visualstatemanager

我的UWP-Application中的ListView出现问题。当第一次选择一个项目时,在MouseOver结束之后它的VisualState会改变为“Selected”,但是当我第二次选择它时,在我选择了另一个项目之后,它甚至在MouseOver之后仍然保持VisualState“Pressed”直到我选择下一个项目。所以我的Item只切换到所需的VisualState一次,以便在会话的其余部分使用此状态。 这可能是造成这种行为的原因,这可能是UWP的独特之处,因为我不记得曾经见过这样的事情。

这是我的ListView,因为我现在在Xaml中使用它:

<ListView IsItemClickEnabled="True"
            Grid.Row="1"
            Grid.Column="1"
            ScrollViewer.VerticalScrollMode="Auto"
            ScrollViewer.VerticalScrollBarVisibility="Auto"
            ItemsSource="{Binding Path=list, Mode=OneWay}"
            ItemTemplate="{StaticResource RepVSmallIcon70ItemTemplate}"
            TabIndex="1"
            SelectedIndex="{Binding Path=selectedIndex, Mode=TwoWay}"
            SelectionChanged="ListView_SelectionChanged"
            IsRightTapEnabled="False"
            ItemContainerStyle="{StaticResource ListViewItemStyleUWP}"/>

使用此ItemContainerStyle:

<Style x:Key="ListViewItemStyleUWP" TargetType="ListViewItem">
    <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/>
    <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}"/>
    <Setter Property="Background" Value="MediumOrchid"/>
    <Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}"/>
    <Setter Property="TabNavigation" Value="Local"/>
    <Setter Property="IsHoldingEnabled" Value="True"/>
    <Setter Property="Padding" Value="12,0,12,0"/>
    <Setter Property="HorizontalContentAlignment" Value="Left"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="MinWidth" Value="160"/>
    <Setter Property="MinHeight" Value="100"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListViewItem">
                <Border x:Name="OuterContainer" RenderTransformOrigin="0.5,0.5">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal">
                                <!-- unselektiert -->
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Duration="0"
                                                                Storyboard.TargetName="ContentBorder"
                                                                Storyboard.TargetProperty="BorderBrush">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="Orange"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Duration="0"
                                                                Storyboard.TargetName="ContentBorder"
                                                                Storyboard.TargetProperty="Background">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="LightBlue"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Duration="0"
                                                                Storyboard.TargetName="ContentBorder"
                                                                Storyboard.TargetProperty="Margin">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="0 0 0 2"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Pressed">
                                <!-- Klick -->
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Duration="0"
                                                                Storyboard.TargetName="ContentBorder"
                                                                Storyboard.TargetProperty="BorderBrush">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="Red"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Duration="0"
                                                                Storyboard.TargetName="ContentBorder"
                                                                Storyboard.TargetProperty="Background">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="Blue"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Duration="0"
                                                                Storyboard.TargetName="ContentBorder"
                                                                Storyboard.TargetProperty="Margin">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="2 2 2 4"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                        <VisualStateGroup x:Name="SelectionStates">
                            <VisualState x:Name="Selected">
                                    <!--Selektiert--> 
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Duration="0"
                                                                Storyboard.TargetName="ContentBorder"
                                                                Storyboard.TargetProperty="BorderBrush">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="DarkRed"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Duration="0"
                                                                Storyboard.TargetName="ContentBorder"
                                                                Storyboard.TargetProperty="Background">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="DarkBlue"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Duration="0"
                                                                Storyboard.TargetName="ContentBorder"
                                                                Storyboard.TargetProperty="Margin">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="0 0 0 2"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Grid x:Name="ReorderHintContent" Background="Transparent">
                        <Border x:Name="ContentContainer">
                            <Border x:Name="ContentBorder"
                                    Background="LightCyan"
                                    BorderBrush="DarkCyan"
                                    BorderThickness="2">
                                <Grid>
                                    <ContentPresenter x:Name="contentPresenter"
                                                        ContentTransitions="{TemplateBinding ContentTransitions}"
                                                        ContentTemplate="{TemplateBinding ContentTemplate}"
                                                        Content="{TemplateBinding Content}"
                                                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                                        Margin="{TemplateBinding Padding}" />
                                </Grid>
                            </Border>
                        </Border>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

1 个答案:

答案 0 :(得分:0)

ListViewItem styles and templates没有SelectionStates VisualStateGroups。因此,ListViewItem样式无法管理selected井中的SelectionStates视图状态。将Selected VisualState移动到CommonStates VisualStateGroup。它会奏效。 更新后的代码如下:

 <Setter Property="Template">
     <Setter.Value>
         <ControlTemplate TargetType="ListViewItem">
             <Border x:Name="OuterContainer" RenderTransformOrigin="0.5,0.5">
                 <VisualStateManager.VisualStateGroups>
                     <VisualStateGroup x:Name="CommonStates">
                         <VisualState x:Name="Normal">
                                    <!--  unselektiert  -->
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Duration="0"
                                                                       Storyboard.TargetName="ContentBorder"
                                                                       Storyboard.TargetProperty="BorderBrush">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Orange" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Duration="0"
                                                                       Storyboard.TargetName="ContentBorder"
                                                                       Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="LightBlue" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Duration="0"
                                                                       Storyboard.TargetName="ContentBorder"
                                                                       Storyboard.TargetProperty="Margin">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="0 0 0 2" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>                      
                         <VisualState x:Name="Pressed">
                                    <!--  Klick  -->
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Duration="0"
                                                                       Storyboard.TargetName="ContentBorder"
                                                                       Storyboard.TargetProperty="BorderBrush">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Red" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Duration="0"
                                                                       Storyboard.TargetName="ContentBorder"
                                                                       Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Blue" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Duration="0"
                                                                       Storyboard.TargetName="ContentBorder"
                                                                       Storyboard.TargetProperty="Margin">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="2 2 2 4" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>   
                         <VisualState x:Name="Selected">
                                    <!--  Selektiert  -->
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Duration="0"
                                                                       Storyboard.TargetName="ContentBorder"
                                                                       Storyboard.TargetProperty="BorderBrush">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="DarkRed" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Duration="0"
                                                                       Storyboard.TargetName="ContentBorder"
                                                                       Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="DarkBlue" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Duration="0"
                                                                       Storyboard.TargetName="ContentBorder"
                                                                       Storyboard.TargetProperty="Margin">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="0 0 0 2" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>                     
                     </VisualStateGroup>                                        
                 </VisualStateManager.VisualStateGroups>
                 <Grid x:Name="ReorderHintContent" Background="Transparent">
                            <Border x:Name="ContentContainer">
                                <Border x:Name="ContentBorder"
                                        Background="LightCyan"
                                        BorderBrush="DarkCyan"
                                        BorderThickness="2">
                                    <Grid>
                                        <ContentPresenter x:Name="contentPresenter"
                                                          Margin="{TemplateBinding Padding}"
                                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                                          Content="{TemplateBinding Content}"
                                                          ContentTemplate="{TemplateBinding ContentTemplate}"
                                                          ContentTransitions="{TemplateBinding ContentTransitions}" />
                                    </Grid>
                                </Border>
                            </Border>
                        </Grid>
             </Border>
         </ControlTemplate>
     </Setter.Value>
 </Setter>