简体   繁体   English

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

[英]How to change Highlight color of the selected ListView item in UWP (Windows 10)

I'm working on a Windows 10 app using C# and XAML. 我正在使用C#和XAML开发Windows 10应用程序。 I have a ListView and I want to change the default HighLight color of an selected item. 我有一个ListView,我想更改所选项目的默认HighLight颜色。 I was seeing many code examples (like this ) but all are designed for WP8 or Win8, I was trying to implement those but they do not work for me. 我看到很多代码示例(像这样 ),但都是为WP8或Win8设计的,我试图实现这些,但它们对我不起作用。

In general I'm having troubles modifying the default themes of controls because I don't find useful documentation. 一般来说,我很难修改控件的默认主题,因为我找不到有用的文档。 It would be great if someone can help me with the highlight color and also recommend me good documentation. 如果有人可以帮助我突出颜色并且还推荐我很好的文档,那将是很棒的。

Actually a better way to discover the styling properties is to use Blend. 实际上,发现样式属性的更好方法是使用Blend。

First, open up your page in Blend. 首先,在Blend中打开您的页面。 Then right click on your ListView and go 然后右键单击ListView并继续

Edit Additional Templates > Edit Generated Item Container (ItemContainerStyle) > Edit a Copy . 编辑其他模板>编辑生成的项目容器(ItemContainerStyle)>编辑副本

Give it a name and hit OK . 给它一个名字然后点击OK

Now, you have generated the full built-in style for your ListViewItem s and this is where you can find all the information about their appearance, animations and other visual behaviors. 现在,您已经为ListViewItem生成了完整的内置样式,您可以在这里找到有关其外观,动画和其他可视行为的所有信息。

You should be look at this piece of code - 你应该看看这段代码 -

<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}" />

See the line SelectedBackground="{ThemeResource SystemControlHighlightListAccentLowBrush}" ? 看看行SelectedBackground="{ThemeResource SystemControlHighlightListAccentLowBrush}" That's where you can apply your own color to it. 这就是你可以应用自己的颜色的地方。 Keep in mind that it should be of type Brush instead of Color . 请记住,它应该是Brush类型而不是Color类型。

If you don't want to use XAML, here is an even easier way (in my opinion) to change these settings, using c#: 如果您不想使用XAML,可以使用c#更简单地(在我看来)更改这些设置:

Application.Current.Resources["SystemControlHighlightListAccentLowBrush"] = new SolidColorBrush(Colors.Red);
Application.Current.Resources["SystemControlHighlightListAccentMediumBrush"] = new SolidColorBrush(Colors.Red);

This way you can really customize your items logically. 这样您就可以在逻辑上自定义项目。

This can be achieved in XAML by overriding the resource. 这可以通过覆盖资源在XAML中实现。

<ListView ...>
    <ListView.Resources>
        <SolidColorBrush x:Key="ListViewItemBackgroundSelected" Color="#FF0000" />
        <SolidColorBrush x:Key="ListViewItemBackgroundSelectedPointerOver" Color="#FF0000" />
    </ListView.Resources>
</ListView>

To extend on bastecklein's Answer. 扩展bastecklein的答案。 You want to use App instead of Application for this method to work in a UWP project. 您希望使用App而不是Application来使此方法在UWP项目中工作。 You can use this code in your App.xaml.cs as you load your initial frame, or you can just set the resource color on the code behind the page that you want to affect. 您可以在加载初始框架时在App.xaml.cs中使用此代码,也可以只在页面后面的代码上设置要影响的资源颜色。

App.Current.Resources["SystemControlHighlightListAccentLowBrush"] = new SolidColorBrush(Colors.Red);
App.Current.Resources["SystemControlHighlightListAccentMediumBrush"] = new SolidColorBrush(Colors.Red);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM