繁体   English   中英

在 Xamarin 中更改 UWP Listview 所选项目背景颜色

[英]Change UWP Listview selected item background color in Xamarin

我需要通过 Xamarin 上的效果删除列表视图的选定背景颜色。 我通过这个链接发现listview中包含资源,但是一直无法通过效果中的控件访问这些资源。

这是我尝试过的:

Windows.UI.Xaml.Controls.ListView listView = (Windows.UI.Xaml.Controls.ListView)Control;
ListView elementListView = (ListView)Element;
var backgroundColor = elementListView.BackgroundColor.ToWindowsColor();
listView.Resources["SelectedBackground"] = new SolidColorBrush(backgroundColor);
listView.Resources["ListViewItemBackgroundSelected"] = new SolidColorBrush(backgroundColor);
listView.Resources["ListViewItemBackgroundSelectedPointerOver"] = new SolidColorBrush(backgroundColor);
listView.Resources["ListViewItemRevealBackgroundSelectedPressed"] = new SolidColorBrush(backgroundColor);
listView.Resources["ListViewItemSelectedBackgroundThemeBrush"] = new SolidColorBrush(backgroundColor);

我不确定这些资源中是否真的包含在 listView 中,因为有关列表视图的 UWP 文档不像按钮或其他控件那样包含有关资源的部分。

有人在这方面取得了成功吗?

编辑

我已经接受了 Cherry Bu - 下面 MSFT 的回答,因为它让我发现我可以使用:

Windows.UI.Xaml.Controls.ListView listView = (Windows.UI.Xaml.Controls.ListView)Control;
ListView elementListView = (ListView)Element;
var backgroundColor = elementListView.BackgroundColor.ToWindowsColor();
listView.Resources["SystemControlHighlightListAccentLowBrush"] = new SolidColorBrush(backgroundColor);
listView.Resources["SystemControlHighlightListAccentMediumBrush"] = new SolidColorBrush(backgroundColor);

在效果上。 这只会更改效果所在的列表视图的资源,而不是应用程序中的所有列表视图。

感谢您的帮助!

如果您想更改 ListView 选定项目的背景,则更简单但不太理想的选择是以应用程序范围的方式覆盖资源。

如果我们提供自定义SystemControlHighlightListAccentLowBrushSystemControlHighlightListAccentMediumBrush ,它将覆盖或使用此 Brush 的实例(甚至包括其他控件)。

为此,我们将转到 UWP 项目的 App.xaml 文件,并将在应用程序的资源字典中添加一个资源:

<Application
x:Class="uwp1.UWP.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:uwp1.UWP"
RequestedTheme="Light">
<Application.Resources>
    <SolidColorBrush x:Key="SystemControlHighlightListAccentLowBrush" Color="Red" />
    <SolidColorBrush x:Key="SystemControlHighlightListAccentMediumBrush" Color="Red" />
</Application.Resources>

这是屏幕截图:

在此处输入图片说明

暂无
暂无

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

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