UWP win10 列表视图选定项样式

本文关键字:样式 视图 win10 列表 UWP | 更新日期: 2023-09-27 18:36:40

没有办法在选择此列表视图项时更改列表视图项的属性?

例如,我希望列表视图项内的矩形在选中时为红色,默认情况下为蓝色。

如何以优雅的方式实现这一目标?

UWP win10 列表视图选定项样式

您可以设置ListView.ItemContainerStyle以自定义ListView中使用的ListViewItems样式。

此页面显示默认样式:https://msdn.microsoft.com/en-us/library/windows/apps/mt299136.aspx

对于您的示例 - 您将在类似于以下内容的代码中更改Selected~Background属性:

<ListView ...>
    <ListView.ItemContainerStyle>
        <Style
            TargetType="ListViewItem">
            <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>

对于以后发现此问题的任何人,我已经通过Nuget中的库解决了这个问题:https://github.com/JerryNixon/Template10.ListHelpers

它的行为为每个状态使用单独的样式。

<Style x:Key="ItemNormalStyle" TargetType="Grid">
    <Setter Property="RequestedTheme" Value="Dark" />
    <Setter Property="Background" Value="{ThemeResource ButtonPointerOverBackgroundThemeBrush}" />
</Style>
<Style x:Key="ItemSelectedStyle" TargetType="Grid">
    <Setter Property="RequestedTheme" Value="Light" />
    <Setter Property="Background" Value="{ThemeResource ButtonBackgroundThemeBrush}" />
</Style>

使用它也非常简单。这是一个附加属性。

<ListView 
    helpers:ListViewHelper.SelectedItemStyle="{StaticResource MySelectorInfo}"
    ItemTemplate="{StaticResource ListViewItemTemplate}" >
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <Setter Property="Padding" Value="0" />
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

祝你好运

我已经在另一个地方回答了这个问题,请检查一下!UWP 网格视图项目选择样式