Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] CollectionView: Color for SelectedItem of CollectionView not set when SelectedItem is assigned in ViewModel #20030

Closed
MAUIoxo opened this issue Jan 20, 2024 · 3 comments
Labels
area-controls-collectionview CollectionView, CarouselView, IndicatorView platform/iOS 🍎 s/needs-info Issue needs more info from the author t/bug Something isn't working

Comments

@MAUIoxo
Copy link

MAUIoxo commented Jan 20, 2024

Description

I have a CollectionView with a ItemTemplate that has a VisualStateManager to define a Color when an Item of the CollectionView is selected:

<VisualStateManager.VisualStateGroups>
    <VisualStateGroup x:Name="CommonStates">
        <VisualState x:Name="Normal">
            <VisualState.Setters>
                <Setter Property="BackgroundColor" Value="Transparent" />
            </VisualState.Setters>
        </VisualState>
        <VisualState x:Name="Selected">
            <VisualState.Setters>
                <Setter Property="BackgroundColor" Value="{StaticResource LightOrange2}" />
            </VisualState.Setters>
        </VisualState>
    </VisualStateGroup>
</VisualStateManager.VisualStateGroups>

When I select one of the items in the CollectionView the item shows the right Color as specified in the section

...
<VisualState x:Name="Selected">

</VisualState>
...

The CollectionView has a Bindingfor theSelectedItem`:

<CollectionView x:Name="PopupCollectionView" ItemsSource="{Binding GroupedSavedStoreItems, Mode=TwoWay}" IsGrouped="True" SelectionMode="Single" SelectedItem="{Binding SelectedSavedStore, Mode=TwoWay}" Margin="20">

The CollectionView is displayed in a custom toolkit:Popup from the CommunityToolkit within a custom Picker Control. When this is opened the CustomPopupContentPage containing this above CollectionView is displayed.

In the ViewModel the first item of the list should be the SelectedItem and is therefore assigned in the ViewModel correspondingly.

On Android it looks fine and the SelectedItem has the right Color when opened:

grafik


On iOS though the Color for a selected item is not applied:

grafik

But, when I search a Store in the above SearchBar an type "A" for instance, the Color is correctly applied:

grafik


The SelectedSavedStore which is bound to the CollectionView is an ObservableProperty and the corresponding GroupedSavedStoreItems which is the bound Collection is also a ObservableProperty and are set in the corresponding CustomPopupViewModel as can be found in the provided example project:

[ObservableProperty]
public ObservableRangeCollection<GroupedSavedStoreItemList> groupedSavedStoreItems;

[ObservableProperty]
public SavedStore selectedSavedStore;

The SelectSavedStore is set in Method SelectSavedStoreByName in the CustomPopupViewModel as follows:

private async Task SelectSavedStoreByName(string savedStoreName)
{
    if (string.IsNullOrWhiteSpace(savedStoreName))
    {
        await SelectSavedStoreByIndex(0);
        return;
    }

    SavedStore foundSavedStore = GroupedSavedStoreItems.SelectMany(list => list).FirstOrDefault(savedStore => savedStore.Name == savedStoreName);

    SelectedSavedStore = foundSavedStore;

    await Task.CompletedTask;
}

Steps to Reproduce

  1. Run the Attached Example Program on Android
  2. Click on the "Down Arrow" to open the CustomPopup

grafik

  1. See that the selected item (SelectedSavedStore from the ViewModel) has the right Color
  2. Run the App on iOS and do the same steps as above
  3. See that the select item (SelectedSavedStorefrom the ViewModel) does not have a Color set which is wrong
  4. Type "A" in the above SearchBar of the CustomPopup and see that the Color is now applied

grafik

Link to public reproduction project repository

SelectedItemColorNotSet_20030

Version with bug

8.0.6-nightly.9880

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

iOS 16.2

Did you find any workaround?

Unfortunately not so far :(

Relevant log output

No response

@MAUIoxo MAUIoxo added the t/bug Something isn't working label Jan 20, 2024
@MAUIoxo MAUIoxo changed the title [iOS] Color for SelectedItem of CollectionView not set when SelectedItem is assigned in ViewModel [iOS] CollectionView: Color for SelectedItem of CollectionView not set when SelectedItem is assigned in ViewModel Jan 21, 2024
@jsuarezruiz jsuarezruiz added platform/iOS 🍎 area-controls-collectionview CollectionView, CarouselView, IndicatorView labels Jan 22, 2024
@PureWeen
Copy link
Member

Yes, this used to work in .NET MAUI

What version of MAUI did this used to work in?

@PureWeen PureWeen added the s/needs-info Issue needs more info from the author label Jan 22, 2024
@ghost
Copy link

ghost commented Jan 22, 2024

Hi @MAUIoxo. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@PureWeen
Copy link
Member

Duplicate of #18933

@PureWeen PureWeen marked this as a duplicate of #18933 Jan 22, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Feb 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-controls-collectionview CollectionView, CarouselView, IndicatorView platform/iOS 🍎 s/needs-info Issue needs more info from the author t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants