【问题标题】:SelectedPointerOver and SelectedPressed do not work for custom ListViewItem style in Windows 10 Universal AppSelectedPointerOver 和 SelectedPressed 不适用于 Windows 10 通用应用程序中的自定义 ListViewItem 样式
【发布时间】:2016-04-26 09:26:37
【问题描述】:

我正在使用this solution 来禁用 ListView 项目容器上的倾斜动画效果。这段代码效果很好,但视觉状态 SelectedPointerOverSelectedPressed 没有被触发:当我将鼠标悬停/按下所选项目时,它不会改变背景颜色。

这是来自 App.xaml 的样式代码:

<Style x:Key="NavigationListViewStyle" TargetType="ListViewItem">
            <Setter Property="Padding" Value="0" />
            <Setter Property="Margin" Value="0" />
            <Setter Property="VerticalContentAlignment" Value="Center" />
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListViewItem">
                        <Grid x:Name="RootGrid">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal" />
                                    <VisualState x:Name="PointerOver">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="PressedBackground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="#FF323A45" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Pressed">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="PressedBackground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="#FF3C4550" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Selected">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="PressedBackground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="#FF46293B" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="SelectedPointerOver">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="PressedBackground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="#FF553549" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="SelectedPressed">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="PressedBackground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="#FF603851" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Rectangle x:Name="PressedBackground" Fill="Transparent" Control.IsTemplateFocusTarget="True"/>
                            <ContentPresenter />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

以及控制代码:

<ListView Grid.Row="0" x:Name="NavigationListView" ItemsSource="{x:Bind NavigationList, Mode=OneTime}" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                          ScrollViewer.VerticalScrollBarVisibility="Auto" ItemContainerStyle="{ThemeResource NavigationListViewStyle}" IsItemClickEnabled="False"
                          SelectionChanged="NavigationListView_SelectionChanged">
                    <ListView.ItemTemplate>
                        <DataTemplate x:DataType="models:MenuItem">
                            <StackPanel Orientation="Horizontal" Padding="15">
                                <FontIcon FontFamily="ms-appx:/Fonts/Icons.ttf#icons" Glyph="{x:Bind GlyphCode}" Foreground="{ThemeResource AccentColorBrush}" VerticalAlignment="Center" />
                                <TextBlock Text="{x:Bind Title}" Margin="30,0,0,0" Foreground="White" />
                            </StackPanel>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

我正在使用带有 VS2015 更新 2 的 Windows SDK 10.1.10586.15。

【问题讨论】:

    标签: xaml win-universal-app windows-10 app.xaml


    【解决方案1】:

    在 UWP 中,ListViewItem 的视觉状态发生了变化。您可以在 Blend 中打开项目并编辑 ItemContainerStyle。将生成以下样式

    <Style TargetType="ListViewItem">
    <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
    <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
    <Setter Property="Background" Value="Transparent"/>
    <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="{ThemeResource ListViewItemMinWidth}"/>
    <Setter Property="MinHeight" Value="{ThemeResource ListViewItemMinHeight}"/>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="ListViewItem">
          <ListViewItemPresenter
              ContentTransitions="{TemplateBinding ContentTransitions}"
              SelectionCheckMarkVisualEnabled="True"
              CheckBrush="{ThemeResource SystemControlForegroundBaseMediumHighBrush}"
              CheckBoxBrush="{ThemeResource SystemControlForegroundBaseMediumHighBrush}"
              DragBackground="{ThemeResource ListViewItemDragBackgroundThemeBrush}"
              DragForeground="{ThemeResource ListViewItemDragForegroundThemeBrush}"
              FocusBorderBrush="{ThemeResource SystemControlForegroundAltHighBrush}"
              FocusSecondaryBorderBrush="{ThemeResource SystemControlForegroundBaseHighBrush}"
              PlaceholderBackground="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}"
              PointerOverBackground="{ThemeResource SystemControlHighlightListLowBrush}"
              PointerOverForeground="{ThemeResource SystemControlHighlightAltBaseHighBrush}"
              SelectedBackground="{ThemeResource SystemControlHighlightListAccentLowBrush}"
              SelectedForeground="{ThemeResource SystemControlHighlightAltBaseHighBrush}"
              SelectedPointerOverBackground="{ThemeResource SystemControlHighlightListAccentMediumBrush}"
              PressedBackground="{ThemeResource SystemControlHighlightListMediumBrush}"
              SelectedPressedBackground="{ThemeResource SystemControlHighlightListAccentHighBrush}"
              DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}"
              DragOpacity="{ThemeResource ListViewItemDragThemeOpacity}"
              ReorderHintOffset="{ThemeResource ListViewItemReorderHintThemeOffset}"
              HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
              VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
              ContentMargin="{TemplateBinding Padding}"
              CheckMode="Inline"/>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
    </Style>
    

    从上面的样式编辑 SelectedPressedBackground SelectedPointerOverBackground of ListViewItemPresenter

    如果你想要视觉状态通过这个Default styles of ListViewItem

    【讨论】:

    • 很高兴它成功了。那么请把答案标记为正确:)
    猜你喜欢
    • 2021-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-07
    • 2016-10-16
    • 2012-07-10
    • 1970-01-01
    • 2016-06-07
    相关资源
    最近更新 更多