如何更改UWP中所选ListView项的突出显示颜色(Windows 10)

ale*_*rdv 23 c# xaml listview windows-10 uwp

我正在使用C#和XAML开发Windows 10应用程序.我有一个ListView,我想更改所选项目的默认HighLight颜色.我看到很多代码示例(像这样),但都是为WP8或Win8设计的,我试图实现这些,但它们对我不起作用.

一般来说,我很难修改控件的默认主题,因为我找不到有用的文档.如果有人可以帮助我突出颜色并且还推荐我很好的文档,那将是很棒的.

Jus*_* XL 21

实际上,发现样式属性的更好方法是使用Blend.

首先,在Blend中打开您的页面.然后右键单击你的ListView去吧

编辑其他模板>编辑生成的项目容器(ItemContainerStyle)>编辑副本.

给它一个名字然后点击OK.

现在,您已经为您的ListViewItems 生成了完整的内置样式,您可以在这里找到有关其外观,动画和其他视觉行为的所有信息.

你应该看看这段代码 -

<ListViewItemPresenter CheckBrush="{ThemeResource SystemControlForegroundBaseMediumHighBrush}" 
                       ContentMargin="{TemplateBinding Padding}" 
                       CheckMode="Inline" 
                       ContentTransitions="{TemplateBinding ContentTransitions}" 
                       CheckBoxBrush="{ThemeResource SystemControlForegroundBaseMediumHighBrush}" 
                       DragForeground="{ThemeResource ListViewItemDragForegroundThemeBrush}" 
                       DragOpacity="{ThemeResource ListViewItemDragThemeOpacity}" 
                       DragBackground="{ThemeResource ListViewItemDragBackgroundThemeBrush}" 
                       DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}" 
                       FocusBorderBrush="{ThemeResource SystemControlForegroundAltHighBrush}" 
                       FocusSecondaryBorderBrush="{ThemeResource SystemControlForegroundBaseHighBrush}" 
                       HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" 
                       PointerOverForeground="{ThemeResource SystemControlHighlightAltBaseHighBrush}"
                       PressedBackground="{ThemeResource SystemControlHighlightListMediumBrush}"
                       PlaceholderBackground="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}"
                       PointerOverBackground="{ThemeResource SystemControlHighlightListLowBrush}"
                       ReorderHintOffset="{ThemeResource ListViewItemReorderHintThemeOffset}" 
                       SelectedPressedBackground="{ThemeResource SystemControlHighlightListAccentHighBrush}"
                       SelectionCheckMarkVisualEnabled="True" 
                       SelectedForeground="{ThemeResource SystemControlHighlightAltBaseHighBrush}"
                       SelectedPointerOverBackground="{ThemeResource SystemControlHighlightListAccentMediumBrush}" 
                       SelectedBackground="{ThemeResource SystemControlHighlightListAccentLowBrush}"
                       VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" />
Run Code Online (Sandbox Code Playgroud)

看到线SelectedBackground="{ThemeResource SystemControlHighlightListAccentLowBrush}"?这就是你可以应用自己的颜色的地方.请记住,它应该是类型Brush而不是Color.


小智 11

如果您不想使用XAML,可以使用c#更简单地(在我看来)更改这些设置:

Application.Current.Resources["SystemControlHighlightListAccentLowBrush"] = new SolidColorBrush(Colors.Red);
Application.Current.Resources["SystemControlHighlightListAccentMediumBrush"] = new SolidColorBrush(Colors.Red);
Run Code Online (Sandbox Code Playgroud)

这样您就可以在逻辑上自定义项目.


小智 9

这可以通过覆盖资源在XAML中实现。

<ListView ...>
    <ListView.Resources>
        <SolidColorBrush x:Key="ListViewItemBackgroundSelected" Color="#FF0000" />
        <SolidColorBrush x:Key="ListViewItemBackgroundSelectedPointerOver" Color="#FF0000" />
    </ListView.Resources>
</ListView>
Run Code Online (Sandbox Code Playgroud)