Skip to content

[2025/05/12] Candidate - In Flight Branch #29335

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

Merged
merged 13 commits into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[Testing] Feature Matrix UITest Cases for CollectionView Selection Fe…
…ature (#29165)

* added the selection feature tests

* added snapshots
  • Loading branch information
LogishaSelvarajSF4525 authored and PureWeen committed May 5, 2025
commit fd44cedb82daf001c56ee41508c717f3d1df58a5
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,10 @@
HorizontalOptions="Center"
AutomationId="ScrollingButton"
WidthRequest="400"/>
<Button Text="Selection FeaturePage"
Clicked="OnSelectionButtonClicked"
HorizontalOptions="Center"
AutomationId="SelectionPageButton"
WidthRequest="400"/>
</VerticalStackLayout>
</ContentPage>
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ private async void OnScrollingButtonClicked(object sender, EventArgs e)
await Navigation.PushAsync(new CollectionViewScrollPage());
}

private async void OnSelectionButtonClicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new CollectionViewSelectionPage());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
using Microsoft.Maui.Graphics;
using Maui.Controls.Sample.CollectionViewGalleries;
using System.Windows.Input;
using System.Collections.Specialized;

namespace Maui.Controls.Sample;

public class Grouping<TKey, TItem> : ObservableCollection<TItem>
{
public TKey Key { get; }
Expand Down Expand Up @@ -62,6 +64,12 @@ public class CollectionViewViewModel : INotifyPropertyChanged
private ObservableCollection<ItemModel> _observableCollection2;
private List<Grouping<string, CollectionViewTestItem>> _groupedList3;
private List<Grouping<string, ItemModel>> _groupedList2;
private SelectionMode _selectionMode = SelectionMode.None;
private object _selectedItem;
private ObservableCollection<object> _selectedItems = new ObservableCollection<object>();
private int _selectionChangedEventCount = 0;
private string _previousSelectionText;
private string _currentSelectionText;

public bool ShowAddRemoveButtons => ItemsSourceType == ItemsSourceType.ObservableCollectionT3 || ItemsSourceType == ItemsSourceType.GroupedListT3;

Expand Down Expand Up @@ -99,6 +107,8 @@ public CollectionViewViewModel()
});

SetItemTemplate();
SelectedItems = new ObservableCollection<object>();
SelectedItems.CollectionChanged += OnSelectedItemsChanged;
}

public object EmptyView
Expand Down Expand Up @@ -270,6 +280,105 @@ public ItemsUpdatingScrollMode ItemsUpdatingScrollMode
}
}

public SelectionMode SelectionMode
{
get => _selectionMode;
set
{
if (_selectionMode != value)
{
_selectionMode = value;
OnPropertyChanged();
}
}
}

public object SelectedItem
{
get => _selectedItem;
set
{
if (_selectedItem != value)
{
_selectedItem = value;
OnPropertyChanged();
OnPropertyChanged(nameof(SelectedItemText));
OnPropertyChanged(nameof(SelectedItemsCount));
}
}
}

public ObservableCollection<object> SelectedItems
{
get => _selectedItems;
set
{
if (_selectedItems != value)
{
_selectedItems = value;
OnPropertyChanged();
OnPropertyChanged(nameof(SelectedItemsCount));
OnPropertyChanged(nameof(SelectedItemText));
}
}
}
public int SelectedItemsCount
{
get
{
if (SelectionMode == SelectionMode.Single)
{
return SelectedItem != null ? 1 : 0;
}
else if (SelectionMode == SelectionMode.Multiple)
{
return SelectedItems?.Count ?? 0;
}
return 0;

}
}
public string SelectedItemText
{
get
{
if (SelectionMode == SelectionMode.Single && SelectedItem is CollectionViewTestItem item)
{
return $"{item.Caption}";
}
else if (SelectionMode == SelectionMode.Multiple && SelectedItems?.Count > 0)
{
var selectedCaptions = SelectedItems
.OfType<CollectionViewTestItem>()
.Select(i => i.Caption);
return string.Join(", ", selectedCaptions);
}
return "No items selected";
}
}

public int SelectionChangedEventCount
{
get => _selectionChangedEventCount;
set
{
_selectionChangedEventCount = value;
OnPropertyChanged();
}
}

public string PreviousSelectionText
{
get => _previousSelectionText;
set { _previousSelectionText = value; OnPropertyChanged(); }
}

public string CurrentSelectionText
{
get => _currentSelectionText;
set { _currentSelectionText = value; OnPropertyChanged(); }
}


private void LoadItems()
{
Expand Down Expand Up @@ -433,6 +542,12 @@ private void SetItemTemplate()
}
}

private void OnSelectedItemsChanged(object sender, NotifyCollectionChangedEventArgs e)
{
OnPropertyChanged(nameof(SelectedItemsCount));
OnPropertyChanged(nameof(SelectedItemText));
}

protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
if (propertyName == nameof(IsGrouped))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?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"
xmlns:local="clr-namespace:Maui.Controls.Sample"
x:Class="Maui.Controls.Sample.CollectionViewSelectionPage"
Title="CollectionViewControlPage">
<ContentPage.ToolbarItems>
<ToolbarItem Text="Options"
Clicked="NavigateToOptionsPage_Clicked"
AutomationId="Options"/>
</ContentPage.ToolbarItems>
<Grid Padding="20"
RowSpacing="10"
ColumnSpacing="10">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>

<!-- CollectionView -->
<CollectionView
x:Name="collectionView"
Grid.Row="0"
Grid.ColumnSpan="2"
ItemsSource="{Binding ItemsSource}"
ItemTemplate="{Binding ItemTemplate}"
ItemsLayout="{Binding ItemsLayout}"
GroupHeaderTemplate="{Binding GroupHeaderTemplate}"
IsGrouped="{Binding IsGrouped}"
SelectionMode="{Binding SelectionMode}"
SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
SelectedItems="{Binding SelectedItems, Mode=TwoWay}"
SelectionChanged="OnCollectionViewSelectionChanged"
AutomationId="CollectionViewControl">
</CollectionView>

<!-- Display Selected Item -->
<Label Text="Selected Item:"
FontSize="14"
FontAttributes="Bold"
Grid.Row="1"
Grid.Column="0"/>
<Label Text="{Binding SelectedItemText}"
AutomationId="SelectedSingle"
FontSize="14"
TextColor="Blue"
Grid.Row="1"
Grid.Column="1"/>

<!-- Display Selected Items Count -->
<Label Text="Selected Items Count:"
FontSize="14"
FontAttributes="Bold"
Grid.Row="2"
Grid.Column="0"/>
<Label Text="{Binding SelectedItemsCount}"
AutomationId="SelectedMultiple"
FontSize="14"
TextColor="Blue"
Grid.Row="2"
Grid.Column="1"/>


<!-- Display SelectionChangedEventArgs -->
<Label Text="SelectionChangedEvent Trigger Count:"
FontSize="14"
FontAttributes="Bold"
Grid.Row="3"
Grid.Column="0"/>
<Label Text="{Binding SelectionChangedEventCount, StringFormat='{0} times'}"
FontSize="14"
TextColor="Blue"
Grid.Row="3"
Grid.Column="1"
AutomationId="SelectionChangedEventCountLabel"/>

<!--Display Previous Selection Text-->
<Label Text="Previous Selection:"
FontSize="14"
FontAttributes="Bold"
Grid.Row="4"
Grid.Column="0"/>
<Label Text="{Binding PreviousSelectionText}"
FontSize="14"
TextColor="Blue"
Grid.Row="4"
Grid.Column="1"
AutomationId="PreviousSelectionTextLabel"/>
<!--Display Current Selection Text-->
<Label Text="Current Selection:"
FontSize="14"
FontAttributes="Bold"
Grid.Row="5"
Grid.Column="0"/>
<Label Text="{Binding CurrentSelectionText}"
FontSize="14"
TextColor="Blue"
Grid.Row="5"
Grid.Column="1"
AutomationId="CurrentSelectionTextLabel"/>
</Grid>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using Microsoft.Maui.Controls;
using System.Collections.ObjectModel;

namespace Maui.Controls.Sample
{
public partial class CollectionViewSelectionPage : ContentPage
{
private CollectionViewViewModel _viewModel;

public CollectionViewSelectionPage()
{
InitializeComponent();
_viewModel = new CollectionViewViewModel();
_viewModel.ItemsSourceType = ItemsSourceType.ObservableCollection5T;
BindingContext = _viewModel;
}

private async void NavigateToOptionsPage_Clicked(object sender, EventArgs e)
{
BindingContext = _viewModel = new CollectionViewViewModel();
_viewModel.ItemsSourceType = ItemsSourceType.ObservableCollection5T;
await Navigation.PushAsync(new SelectionOptionsPage(_viewModel));
}

void OnCollectionViewSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (BindingContext is CollectionViewViewModel vm)
{
vm.SelectionChangedEventCount++;

var previousSelection = e.PreviousSelection.Any()
? string.Join(", ", e.PreviousSelection.OfType<CollectionViewViewModel.CollectionViewTestItem>().Select(item => item.Caption))
: "No previous items";

var currentSelection = e.CurrentSelection.Any()
? string.Join(", ", e.CurrentSelection.OfType<CollectionViewViewModel.CollectionViewTestItem>().Select(item => item.Caption))
: "No current items";
vm.PreviousSelectionText = $"{previousSelection}";
vm.CurrentSelectionText = $"{currentSelection}";
}
}
}
}
Loading