OXO
OXO

Reputation: 1098

Color for SelectedItem of CollectionView not set when SelectedItem is assigned in ViewModel

I have a .NET MAUI App in which I us a CustomPopup from the CommunityToolkit. When it opens, I have a CollectionView with ObservableProperties that Bind to it.

There is also a SelectedItem Binding on the CollectionView to its ViewModel.

I also set a VisualStateGroup to specify the Color for a selected item:

<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 do set the SelectedSavedStore in in the ViewModel I can only see the Color set on Android, but not on iOS:

Android:

enter image description here



iOS:

enter image description here

Right now, I think it is a Bug and wrote the following isse:

https://github.com/MAUIoxo/SelectedItemColorNotSet_20030

But, I am also not sure if this is a Bug or if I am doing something stupid here and just Android still accepts it.

Would it be possible to have some more eyes on it to be save for that?

The ContentPage with the correpsonding Bindings is located in: ..\Pages\Views\Controls\CustomPicker\CustomPopupContentPage.xaml.cs

My corresponding ViewModel is located in: ..\ViewModels\CustomPopupViewModel.cs The selecttion is done in SelectSavedStoreByName(..) with SelectedSavedStore = foundSavedStore;

Am I doing something wrong here?



Hint:

Upvotes: 0

Views: 322

Answers (1)

Liqun Shen-MSFT
Liqun Shen-MSFT

Reputation: 8220

Yes I agree with you. I can reproduce your issue.

After searching on GitHub, seems related to [iOS] CollectionView multi-select pre-selected item VisualState error #13072 and Custom selection styles for items in CollectionView are ignored when programmatically selecting an item #18933. You may follow up this issue.

After debugging, I found that the value of SelectedSavedStore has been set correctly but UI doesn't update.

So, I come up with a workaround (but in a little awkward way).

    private async void OnDropDownButtonClicked(object sender, EventArgs e)
    {
        ...
        customPickerPopup = new CustomPopup();

        //Here we first set the SelectedSavedStore to null
        SavedStore selected = customPopupViewModel.SelectedSavedStore;
        customPopupViewModel.SelectedSavedStore = null;
        customPickerPopup.BindingContext = customPopupViewModel;

        ...
        //And after a delay, we set it back to the right value
        mainPage.ShowPopupAsync(customPickerPopup);
        await Task.Delay(50);
        
        customPopupViewModel.SelectedSavedStore = selected;
        customPopupViewModel.OnPropertyChanged("SelectedSavedStore");
    }

Hope it helps!

Upvotes: 0

Related Questions