-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Custom selection styles for items in CollectionView are ignored when programmatically selecting an item #18933
Comments
I also found the same problem. This problem does not seem to appear on Android. |
Verified this on Visual Studio Enterprise 17.9.0 Preview 2(8.0.3). Repro on Windows 11, Android 14.0-API34, iOS 17.0 and MacCatalyst with below Project: Windows: When the CollectionView loads, the second item is selected with the default selection style -> Clicking a different item correctly applies the custom selection style. Android: Applying selection styles to multiple items iOS: When the CollectionView loads, the second item is disappeared, and applying selection styles to multiple items. |
ListView appears to be similarly affected :( |
HorizontalScrollBarVisibility="Never"
} First item is selected as default but no color on selected background how to fix it .. I think it is bug ... |
The same for Android |
Any update? <VisualState Name="Selected">
<VisualState.Setters>
<Setter TargetName="nameLabel" Property="Label.TextColor" Value="White" />
<Setter Property="BackgroundColor" Value="{DynamicResource BrandPrimaryColor}" />
</VisualState.Setters>
</VisualState> |
Visual Studio 2022, Windows platform, Same issue. :( I added a ListViewObj_PropertyChanged function and when the Answer property changes I manually update a Label to show the content of the selected item. This way I can see when the values change so I can say that when the page loads Answer has a value of null so there is nothing highlighted. After a short delay I manually set the value and the Label updates but the color remains unchanged. I then click on something else and the Label updates but the color still remains unchanged. I then click on the first value again and from now on the colors change perfectly. |
Are you able to share some of the implementation for this? I've tried putting a delay timer that starts when the pages appearing method is called. Sometimes, it works but not always, android seems to respond better. |
I'm having problems in removing the selecteditem background color in Windows (WinUI). I tried with styles: and this in Platforms\Windows\App.xaml: |
Hello, after spending a lot of time struggling with a lot of weird issues even with the most basic usage of CollectionView (especially on Windows) I decided to implement a CustomCollectionView control based on custom VisualStates and GestureRecognizers. It is very basic and you probably will have to modify it to make it suit your needs but if you currently are where I was just a few months ago struggling to learn MAUI/XAML, it will at least give you a good base to move forward with a solution that works for you. |
Is this actually a bug with iOS or do you just need to delay preselection until everything is fully loaded? I got this working for styles to show on programmatically preselecting an item in Using Prism's OnAppearing from our VM and the Init method but you can do this from code behind methods also private void collectionView_Loaded(object? sender, System.EventArgs e)
{
if (CollectionView is not null)
{
_ = _dispatcher.Dispatch(async () =>
{
await Task.Delay(400).ConfigureAwait(true);
CollectionView.SelectedItem = null;
CollectionView.SelectedItem = ViewModels.FirstOrDefault() // Get which ever view model to preselect
});
}
}
public override void Initialize(INavigationParameters parameters)
{
base.Initialize(parameters);
CollectionView.Loaded += collectionView_Loaded;
}
public override void OnAppearing()
{
base.OnAppearing();
if (FirstLoad)
{
FirstLoad = false;
ViewModels.ReplaceRange(); // Load the Collection on page Appearing
}
} |
Description
Programmatically selecting an item in a CollectionView does not respect the VisualStateManager in its ItemTemplate (applied by styles or otherwise). Selecting the item as a user (tap / click) works.
Steps to Reproduce
Create a new MAUI app and replace default views with a CollectionView. Set its SelectionMode to "Single." Subscribe to its "Loaded" event. Add a VisualStateManager to its ItemTemplate that changes the BackgroundColor to Aqua when Selected.
Assign a list of strings to the CollectionView's ItemSource and, when its Loaded event fires, set the CollectionView's SelectedItem to the second string from the list.
On Windows: When the CollectionView loads, the second item is selected with the default selection style:
Clicking a different item correctly applies the custom selection style:
Selecting a different item doesn't bring it back:
And when selecting a different item again, either the SelectionMode or VisualStateManager breaks, applying selection styles to multiple items:
Link to public reproduction project repository
https://github.com/tschbc/SelectionStyleIssue
Version with bug
8.0.3
Is this a regression from previous behavior?
Not sure, did not test other versions
Last version that worked well
Unknown/Other
Affected platforms
iOS, Windows
Affected platform versions
windows10.0.19041.0, iOS 17.0
Did you find any workaround?
No
Relevant log output
No response
The text was updated successfully, but these errors were encountered: