1

I am trying to customize the selection color of a cell in a CollectionView but no matter how I try it, it's always an ugly grey.

I want my item template to have rounded corners, but when I select an item the I see ugly square grey corners behind it, as in this image:

ugly gray corners

Here's my current XAML:

<?xml version="1.0" encoding="UTF-8"?>
<ContentView
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Tests.CollectionViewTest">
<ContentView.Content>
    <CollectionView
        x:Name="collectionView"
        Margin="15,0"
        ItemSizingStrategy="MeasureFirstItem"
        Grid.Row="1"
        Grid.RowSpan="2"
        VerticalScrollBarVisibility="Never"
        BackgroundColor="Transparent"
        SelectionMode="Multiple"
        HorizontalOptions="Center"
        VerticalOptions="Center"
        >

        <CollectionView.ItemsLayout>
            <GridItemsLayout
                Orientation="Vertical"
                HorizontalItemSpacing="1"
                VerticalItemSpacing="1"
                Span="3" />
        </CollectionView.ItemsLayout>
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <Frame
                    x:Name="selectionFrame"
                    CornerRadius="18"
                    BackgroundColor="Transparent"
                    Padding="0"
                    HasShadow="False"
                    IsClippedToBounds="True"
                    BorderColor="Transparent">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup
                            Name="CommonStates">
                            <VisualState
                                Name="Normal" />
                            <VisualState
                                Name="Focused">
                                <VisualState.Setters>
                                    <Setter
                                        Property="BackgroundColor"
                                        Value="Transparent" />
                                </VisualState.Setters>
                            </VisualState>
                            <VisualState
                                Name="Selected">
                                <VisualState.Setters>
                                    <Setter
                                        Property="BackgroundColor"
                                        Value="#e25fc4" />
                                </VisualState.Setters>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <StackLayout
                        BackgroundColor="#f7f0f6"
                        HorizontalOptions="FillAndExpand"
                        VerticalOptions="FillAndExpand"
                        Orientation="Vertical"
                        Padding="8,0,8,10"
                        Margin="10"
                        Spacing="0"
                        HeightRequest="100">
                        <Label
                            Padding="10"
                            x:Name="ServiceName"
                            BackgroundColor="Transparent"
                            Text="Some Text"
                            HorizontalTextAlignment="Center"
                            TextColor="HotPink"
                            FontSize="Micro"
                            FontAttributes="Bold"
                            HorizontalOptions="Center"
                            VerticalOptions="End" />
                        <Label
                            BackgroundColor="Transparent"
                            Text="Some More Text"
                            HorizontalTextAlignment="Center"
                            TextColor="HotPink"
                            FontSize="Micro"
                            HorizontalOptions="Center"
                            VerticalOptions="Start" />
                    </StackLayout>
                </Frame>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
</ContentView.Content>
</ContentView>

And my code-behind:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Xamarin.Forms;

namespace Tests
{
    public partial class CollectionViewTest : ContentView
    {
        public CollectionViewTest()
        {
            InitializeComponent();
            collectionView.ItemsSource = new ObservableCollection<string>()
            {
                "", "", "", "", "", "", "", "", "", "", "", "", "", ""
            };
        }
    }
}

I've tried other ways of doing this too, but nothing worked.

Is there a way to do it, or is this just a bug with CollectionView?

Cfun
  • 8,442
  • 4
  • 30
  • 62
Le Mot Juiced
  • 3,761
  • 1
  • 26
  • 46
  • What platform? Anyway, I think a work-around is to wrap a frame inside another frame. The outer frame should be square (no rounded corners), and give it the background color you want. The inner frame has the rounded corners, and transparent background. Not sure exactly how to set that up with visual state manager and select, but that may give you an idea. (If you get this to work, add an answer (don't edit the question; add an actual answer below) showing the working code. This will help others. After 48 hours, select your own answer.) – ToolmakerSteve May 01 '21 at 03:27
  • Is the issue in ios ? – Leo Zhu May 03 '21 at 02:02
  • @ToolMakerSteve I need the background transparent. – Le Mot Juiced May 03 '21 at 18:13
  • @LeoZhu-MSFT yes it's in iOS – Le Mot Juiced May 03 '21 at 20:40
  • @LeMotJuiced - it isn't clear where that grey is coming from - maybe it isn't due to being selected. What happens if you set CornerRadius on the original, not-selected, button state? If you see grey around it, then you have to try different experiments to find out where that grey is from. If you DON'T see grey, then your title is correct - it is a problem with the selected state. In which case, I don't know how to fix it. – ToolmakerSteve May 04 '21 at 19:58
  • @ToolmakerSteve when an item with rounded corners is not selected, the gray area is not visible. The gray area appears and disappears based on the selection state. – Le Mot Juiced May 07 '21 at 16:08
  • Sorry, I had misread the XAML. It looks like there is an issue when a selected frame has rounded colors. The "background color" applies only within the "rounded" area, when corners are rounded. What I tried to explain in first comment, is that i believe you can avoid this: Create an outer frame that is NOT rounded. This is what gets selected, so you set its background color = `transparent`, overriding the gray. INSIDE you place another frame, with rounded corners, and the hot pink color. – ToolmakerSteve May 07 '21 at 21:10
  • That is a clever workaround, but unfortunately, in my real application the collection view is in front of a gradient background, so it’s not so simple to place a masking frame in front of that. I’m not sure how I would do that at all, in fact. – Le Mot Juiced May 09 '21 at 02:26

2 Answers2

1

I found a kludgey solution, and in the absence of one that works the right way, it will have to do.

  1. Set selection behavior in the CollectionView to none.
  2. Put a tapGestureRecognizer into the itemTemplate
  3. To simulate selection states, in the event handler for the tapGestureRecognizer, cast the sender to a Frame (or whatever element you attached the gesture recognizer to) and turn the frame border on or off (or do whatever you need to for your own custom selected-state appearance).
  4. Manually handle whatever would normally be triggered by the CollectionView in response to selections. In other words, if you can select multiple items, you might be tracking the selected items in a separate list, and you will now have to do that from inside the tapGestureRecognizer.

It’s wrong but it works and sometimes that’s how you gotta do.

Le Mot Juiced
  • 3,761
  • 1
  • 26
  • 46
0

I start again to try to make it work, now i have a Frame in a StackLayout. And not a the other way around. But no luck is see , now there are no corners around the selected item.Sorry but i cannot make it to work.

Bas H
  • 2,114
  • 10
  • 14
  • 23
  • I was so excited to try this. Did it work for you? It didn't change anything for me. – Le Mot Juiced Apr 30 '21 at 21:53
  • Try to change CollectionView in Frame. Docs are here .https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/collectionview/selection#change-selected-item-color – Bas H May 01 '21 at 06:08
  • It changes the background color of the Frame but it doesn't remove the ugly gray selection box – Le Mot Juiced May 03 '21 at 20:50