【问题标题】:Change UWP Listview selected item background color in Xamarin在 Xamarin 中更改 UWP Listview 选定项背景颜色
【发布时间】:2020-06-27 17:39:08
【问题描述】:

我需要通过 Xamarin 上的效果删除列表视图的选定背景颜色。我通过这个link发现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 中,因为关于 listview 的 UWP 文档没有像 Button 或其他控件那样包含有关资源的部分。

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

编辑

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

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

感谢您的帮助!

【问题讨论】:

  • 您提供的链接更改了所述 ListViewItem 使用的主题资源值(这可能是矫枉过正,但事实并非如此)。在这种情况下,您应该使用 App.Current.Resources["SelectedBackground"] = new SolidColorBrush(backgroundColor);而是 listView.Resources["SelectedBackground"] = new SolidColorBrush(backgroundColor);

标签: c# xamarin xamarin.forms uwp


【解决方案1】:

如果您想更改 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>

这是截图:

【讨论】:

  • 在我的测试中,SystemControlHighlightListAccentLowBrush 是选定项的背景,其中SystemControlHighlightListAccentMediumBrush 是悬停在已选定项上时的背景颜色。我使用了相同的颜色,但增加了不透明度。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-03
相关资源
最近更新 更多