jlf0dev
jlf0dev

Reputation: 115

Change UWP Listview selected item background color in Xamarin

I need to remove the selected background color of a listview through an effect on Xamarin. I have found that the listview contains resources through this link, but I haven't been able to access these resources through the control in the effect.

Here is what I have tried:

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

I'm not sure if any of these resources are actually contained in the listView because the UWP documentation on listviews don't contain a section on resources like it does for Buttons or other controls.

Anyone had any success with this?

Edit

I've accepted Cherry Bu - MSFT's answer below as it led to me discover I can use:

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

in the effect. This will only change the resource for the listview the effect is placed on and not all listviews in the application.

Thank you for your help!

Upvotes: 1

Views: 1038

Answers (1)

Cherry Bu - MSFT
Cherry Bu - MSFT

Reputation: 10346

If you want to change ListView selected items background, the easier but far less ideal option you have is to override the resource in an application-wide manner.

If we provide a custom SystemControlHighlightListAccentLowBrush and SystemControlHighlightListAccentMediumBrush, it will override or instances where this Brush is used (even including other controls).

To do this, we will go to the UWP project’s App.xaml file and will add a resource in the Application‘s resource dictionary:

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

This is the screenshot:

enter image description here

Upvotes: 1

Related Questions