在Xamarin中更改UWP列表视图选定项目的背景颜色

irlmq6kh  于 2023-05-21  发布在  其他
关注(0)|答案(1)|浏览(111)

我需要通过Xamarin的效果来移除列表视图的选定背景颜色。我发现列表视图包含通过此link的资源,但我还无法通过效果中的控件访问这些资源。
以下是我尝试过的:

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中是否真的包含这些资源,因为关于listview的UWP文档不像Buttons或其他控件那样包含有关资源的部分。
有人成功了吗?

编辑

我已经接受了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);

在效果。这只会更改效果所在的列表视图的资源,而不会更改应用程序中的所有列表视图。
谢谢你的帮助!

kpbwa7wx

kpbwa7wx1#

如果要更改ListView选定项的背景,更简单但不太理想的选择是以应用程序范围的方式重写资源。
如果我们提供一个自定义的SystemControlHighlightListAccentLowBrushSystemControlHighlightListAccentMediumBrush,它会覆盖或使用此Brush的示例(甚至包括其他控件)。
为此,我们将转到UWP项目的App.xaml文件,并在Application的资源字典中添加一个资源:

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

这是截图:

相关问题