On iOS
When using a CollectionView, overriding the selected visual states of the DataTemplate content the Gray SelectBackgroundView is shown and cannot be hidden. A long press will show the Gray selectedbackground color, then the overridden one which looks odd.
Additionally, if the content doesn't fill the whole cell(such as using rounded corners) the gray background selection shows through on the unused space which looks bad(see below)
On Android
The same thing happens but it uses the system selection colour if the overriden background colour is Transparent.
<DataTemplate>
<Grid Margin="10" BackgroundColor="Transparent" HeightRequest="200">
<VisualStateManager.VisualStateGroups>
<VisualStateGroupList>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="Selected">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="Transparent" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</VisualStateManager.VisualStateGroups>
The justification is that i want to handle the selection change within the visible content and there is no way to stop the full cell background colour(Gray on iOS and System colour on android) being shown and too leave selection to the templated content.
This is the code on iOS that is causing the problem
When the item is selected the unused background will not change colour
The unused background shows either Gray(iOS) and system color on Android
You could provide a property of CollectionView to set the selected item colour, or have some other way of disabling it. I could disable selection and add a tap gesture to the sub-views but that would be more work and would lose some of the benefits of CollectionView.
@newky2k There was a discussion on the selected item background color somewhere on Github, but I can't find it, particularly with respect to overriding Android's default style colors. @hartez
Another approach here could possibly be adding content insets to CollectionView along with item spacing, which would not show the gray/orange selection colors(https://github.com/xamarin/Xamarin.Forms/issues/7442), but it would restrict the tappable item area.
I agree, however, that VSM should work as intended.
I have to confirm this one. Happens also here on my end.
EDIT: In my case, I was able to work around the behavior using a Grid and setting the background in both cases to the page background...
On Android, you can make it transparent by setting
<item name="android:colorActivatedHighlight">@android:color/transparent</item>
Does someone knows a renderer stuff to achieve transparency on iOS?
@eli191 Ive followed as suggested in the docs on Xamarin using the visual state manage but doesnt work, I havnt been able to find work around yet. My problem is my background is a blurred image so I cannot simply change the row to the same as the background source.
Below does not work:
<VisualStateManager.VisualStateGroups>
<VisualStateGroup Name="CommonStates">
<VisualState Name="Normal" >
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="Transparent" />
</VisualState.Setters>
</VisualState>
<VisualState Name="Selected">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="Transparent" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
@LeoJHarris I ended up with SelectionMode="None"
and custom GestureRecognizer
like this for example
<DataTemplate>
<StackLayout
InputTransparent="False">
<syncEffectsView:SfEffectsView
TouchDownEffects="Ripple"
RippleColor="{DynamicResource AccentColor}"
CornerRadius="8"
InputTransparent="True">
<Label
InputTransparent="True"
Padding="5,15"
HorizontalOptions="Center"
VerticalOptions="Center"
Text="{Binding}"
TextColor="{DynamicResource TextColor}"
FontSize="12">
</Label>
</syncEffectsView:SfEffectsView>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer
Command="{prism:NavigateTo 'NewPage'}">
<TapGestureRecognizer.CommandParameter>
<prism:NavigationParameters>
<prism:NavigationParameter
Key="Book"
Value="{Binding ViewModel.SelectedBook.BookName, Source={x:Reference book_page}}" />
<prism:NavigationParameter
Key="BookId"
Value="{Binding ViewModel.SelectedBookId, Source={x:Reference book_page}}" />
</prism:NavigationParameters>
</TapGestureRecognizer.CommandParameter>
</TapGestureRecognizer>
</StackLayout.GestureRecognizers>
</StackLayout>
</DataTemplate>
@eli191 I thought about doing that, not a biggie atm to have the gray background color on selected rows. We can wait it out if they fix this issue soon otherwise Ill look at implementing something similiar to that.
This really needs fixing.
Hi wondering if this will be fixed, whatever I do I cannot remove the gray background.Any suggestion
Most helpful comment
I have to confirm this one. Happens also here on my end.
EDIT: In my case, I was able to work around the behavior using a Grid and setting the background in both cases to the page background...