.Net MAUI - CollectionView->SelectedItems property doesn't work properly on Windows
Hi!
I'm working on a project with .Net MAUI, and I have some problems with the CollectionView item, binded with a List<string> already populated. The Binding on the "SelectedItems" property is with another List<string> already populated, and if "Groups" contains an element of "SelectedGroups", it has to display the item with the checkbox selected (in Windows, if I use a CollectionView, it shows a checkbox by default). The problem is that with the Android emulator, this and the code behind are working properly, also it doesn't show a checkbox, but it highlights the item, but for Windows it's not working. How can I fix it?
Here is the .xaml and the .cs code of the "CollectionView_SelectionChanged" method:
XAML
<ScrollView x:Name="scrollView">
<CollectionView ItemsSource="{Binding Groups}"
SelectionChanged="CollectionView_SelectionChanged"
BackgroundColor="Grey"
SelectionMode="Multiple"
SelectedItems="{Binding SelectedGroups, Mode=TwoWay}">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="{x:Type x:String}">
<Label Text="{Binding .}"
Margin="5"
VerticalTextAlignment="Center"/>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</ScrollView>
CS
private void CollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.CurrentSelection.Count > 0)
{
foreach (var g in healthEditConfigFormViewModel.Groups)
{
if (e.CurrentSelection.Contains(g)
&& !healthEditConfigFormViewModel.PathConfiguration.Groups.Contains(g))
healthEditConfigFormViewModel.PathConfiguration.Groups.Add(g);
else if(!e.CurrentSelection.Contains(g)
&& healthEditConfigFormViewModel.PathConfiguration.Groups.Contains(g))
healthEditConfigFormViewModel.PathConfiguration.Groups.Remove(g);
}
}
HealthEditConfigFormViewModel.cs
using System.Collections.ObjectModel;
namespace BeholderDemo.ViewModel.Configurations
{
[QueryProperty("HealthDomain", "HealthDomain")]
[QueryProperty("PathConfiguration", "PathConfiguration")]
[QueryProperty("Groups", "Groups")]
public partial class HealthEditConfigFormViewModel : ObservableObject
{
[ObservableProperty]
HealthDomain healthDomain;
[ObservableProperty]
List<string> groups;
[ObservableProperty]
PathConfiguration pathConfiguration;
[ObservableProperty]
public List<HeaderDictionary> headerDictionary;
[ObservableProperty]
public ObservableCollection<HeaderDictionary> listViewDictionary;
[ObservableProperty]
ObservableCollection<object> selectedGroups;
private readonly IManager _manager;
public HealthEditConfigFormViewModel(IManager manager)
{
_manager = manager;
headerDictionary = new List<HeaderDictionary>();
listViewDictionary = new();
selectedGroups = new();
}
public void GetHeaders()
{
if (pathConfiguration.Headers.Count >0)
{
foreach (var x in pathConfiguration.Headers)
headerDictionary.Add(new() { Key = x.Key, Value = x.Value });
}
else
headerDictionary.Add(new() { Key="", Value="" });
}
//[RelayCommand]
public async void Save()
{
var config = healthDomain.Configurations.Find(c => c.Id == pathConfiguration.Id);
if (config!=null)
{
healthDomain.Configurations[healthDomain.Configurations.IndexOf(config)] = pathConfiguration;
}
else
healthDomain.Configurations.Add(pathConfiguration);
await _manager.ConfigurationManager.UpdateDomain(healthDomain, healthDomain.Id);
await Shell.Current.DisplayAlert("Salvataggio...", $"Modifiche salvate con successo.", "Ok");
//await Shell.Current.Navigation.PopAsync();
//await Shell.Current.GoToAsync(nameof(HealthEditForm),true, new Dictionary<string,object>()
//{
// {"Domain", healthDomain }
//});
}
[RelayCommand]
public void PathChanged(string[] data)
{
string oldPath = data[1],
newPath = data[0];
if (pathConfiguration.Path== oldPath)
pathConfiguration.Path= newPath;
}
[RelayCommand]
public void AddHeader(HeaderDictionary headerDictionary)
{
if (listViewDictionary.Contains(headerDictionary))
{
listViewDictionary[listViewDictionary.IndexOf(headerDictionary)] = headerDictionary;
pathConfiguration.Headers[headerDictionary.Key] = headerDictionary.Value;
}
else
ListViewDictionary.Add(headerDictionary);
pathConfiguration.Headers.Add(headerDictionary.Key, headerDictionary.Value);
}
}
}
Thanks. `