uwp win10 Listview SelectedItem样式

时间:2016-02-29 17:07:33

标签: c# windows win-universal-app

有没有办法在选择这个时更改ListviewItem的属性?

作为一个例子,我希望ListviewItem中的一个矩形在选中时为红色,默认为蓝色。

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

3 个答案:

答案 0 :(得分:16)

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

此页面显示默认样式: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>

答案 1 :(得分:0)

对于以后找到它的任何人,我已经在Nuget中的一个库中解决了这个问题:https://github.com/JerryNixon/Template10.ListHelpers

它的行为针对每个状态使用不同的样式。

false

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

<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>

//祝你好运

答案 2 :(得分:-2)

我已经在另一个地方回答了这个问题,请检查一下! UWP gridview item selection style