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