Skip to content

MAUI - CollectionView Pre Select Item background color not change on windows #15666

Closed

Description

Description

If we set the SelectedItem for CollectionView before loading the page and also set the selected backgroundcolor , the collectionview will not render the background color for the SelectedItem for the CollectionView on window, but it works properly on android.

Steps to Reproduce

1.download the official sample here : https://github.com/dotnet/maui-samples/tree/main/7.0/UserInterface/Views/CollectionViewDemos
2.Add the following code to VerticalListSinglePreSelectionPage.xaml

     <ContentPage.Resources>
    <Style TargetType="Grid">
        <Setter Property="VisualStateManager.VisualStateGroups">
            <VisualStateGroupList>
                <VisualStateGroup x:Name="CommonStates">
                    <VisualState x:Name="Normal" />
                    <VisualState x:Name="Selected">
                        <VisualState.Setters>
                            <Setter Property="BackgroundColor" 
                                    Value="LightSkyBlue" />
                        </VisualState.Setters>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateGroupList>
        </Setter>
    </Style>
</ContentPage.Resources>

Then the whole code will like this:

<?xml version="1.0" encoding="UTF-8"?> 
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             x:Class="CollectionViewDemos.Views.VerticalListSinglePreSelectionPage"
             Title="Vertical list (single pre-selection)">

    <ContentPage.Resources>
        <Style TargetType="Grid">
            <Setter Property="VisualStateManager.VisualStateGroups">
                <VisualStateGroupList>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Normal" />
                        <VisualState x:Name="Selected">
                            <VisualState.Setters>
                                <Setter Property="BackgroundColor" 
                                        Value="LightSkyBlue" />
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateGroupList>
            </Setter>
        </Style>
    </ContentPage.Resources>
    <StackLayout Margin="20">
        <Label Text="{Binding SelectedMonkeyMessage}" />
        <CollectionView ItemsSource="{Binding Monkeys}"
                        SelectionMode="Single"
                        SelectedItem="{Binding SelectedMonkey}"
                        SelectionChangedCommand="{Binding MonkeySelectionChangedCommand}">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <Grid Padding="10">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <Image Grid.RowSpan="2" 
                               Source="{Binding ImageUrl}" 
                               Aspect="AspectFill"
                               HeightRequest="60" 
                               WidthRequest="60" />
                        <Label Grid.Column="1" 
                               Text="{Binding Name}" 
                               FontAttributes="Bold" />
                        <Label Grid.Row="1"
                               Grid.Column="1" 
                               Text="{Binding Location}"
                               FontAttributes="Italic" 
                               VerticalOptions="End" />
                    </Grid>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </StackLayout>
</ContentPage>

and modify method MonkeySelectionChanged in class MonkeysViewModel.cs

       void MonkeySelectionChanged() 
    {
        if (SelectedMonkey != null) {

            SelectedMonkeyMessage = $"Selection {selectionCount}: {SelectedMonkey.Name}";
            OnPropertyChanged("SelectedMonkeyMessage");
            selectionCount++;
        }

3.deploy the app to windows device and observe the bug.

We will find that the SelectedItem is showing on the top of page VerticalListSinglePreSelectionPage,but the BackgroundColor is not changed on windows machine. But on android device, it all works properly.

Link to public reproduction project repository

https://github.com/dotnet/maui-samples/tree/main/7.0/UserInterface/Views/CollectionViewDemos

Version with bug

7.0.49

Last version that worked well

6.0

Affected platforms

Windows

Affected platform versions

windows

Did you find any workaround?

No response

Relevant log output

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions