dotnet / dotnet/maui

Raise event Always user tap a row in collectionview

Open
#28,525 6 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

area-controls-collectionview proposal/open
Dominant language
C#
Stars
23.3k
Forks
2k
Avg merge
1d 14h
Merged PRs (30d)
296

Description

Description

Raise event Always user tap a row in collectionview

Today we have tree options with SelectionChanged: none, single and multiple.
I want to propose a fourth: "ItemTapped"
I need this because using "single" the SelectionChanged event not fire when the user tap again on an already selected item.
It's expected, ok, but with Listview we have an option to raise event on every tap.

The workaround to SelectedItem = null erase the color of the last item selected. So we can try to colorize the row manually, but when used with autogrow there's a bug that is painting the row on every page and sometimes it return the incorrect item tapped.

So, this is why I propose the ItemTapped native event.

It's simple to implement, just not return when the previousSelection[0] == newSelection[0] on SelectableItemsView.cs

Today we have:

if (previousSelection.Count == newSelection.Count)
{
	if (previousSelection.Count == 0 || (previousSelection[0] == newSelection[0]))
	{
		// Both selections are empty or have the same single item; no reason to signal a change
		return;
	}
}

I propose:


if (newMode != SelectionMode.ItemTapped && previousSelection.Count == newSelection.Count)
{
	if (previousSelection.Count == 0 || (previousSelection[0] == newSelection[0]))
	{
		// Both selections are empty or have the same single item; no reason to signal a change
		return;
	}
}


Public API Changes

https://github.com/dotnet/maui/blob/86793f41ed3ae097c14eb9bee045e90f6d4c0444/src/Controls/src/Core/Items/SelectableItemsView.cs

static void SelectionModePropertyChanged(BindableObject bindable, object oldValue, object newValue)
		{
			var selectableItemsView = (SelectableItemsView)bindable;

			var oldMode = (SelectionMode)oldValue;
			var newMode = (SelectionMode)newValue;

			IList<object> previousSelection = new List<object>();
			IList<object> newSelection = new List<object>();

			switch (oldMode)
			{
				case SelectionMode.None:
					break;
case SelectionMode.ItemTapped:
				case SelectionMode.Single:
					if (selectableItemsView.SelectedItem != null)
					{
						previousSelection.Add(selectableItemsView.SelectedItem);
					}
					break;
				case SelectionMode.Multiple:
					previousSelection = selectableItemsView.SelectedItems;
					break;
			}

			switch (newMode)
			{
				case SelectionMode.None:
					break;
case SelectionMode.ItemTapped:
				case SelectionMode.Single:
					if (selectableItemsView.SelectedItem != null)
					{
						newSelection.Add(selectableItemsView.SelectedItem);
					}
					break;
				case SelectionMode.Multiple:
					newSelection = selectableItemsView.SelectedItems;
					break;
			}

			if (newMode != SelectionMode.ItemTapped && previousSelection.Count == newSelection.Count)
			{
				if (previousSelection.Count == 0 || (previousSelection[0] == newSelection[0]))
				{
					// Both selections are empty or have the same single item; no reason to signal a change
					return;
				}
			}

			var args = new SelectionChangedEventArgs(previousSelection, newSelection);
			SelectionPropertyChanged(selectableItemsView, args);
		}


Intended Use-Case

My program opens a window for the user to select a product.
When tapping on the item, the previous window loads the selected item on the screen.
If the user saves the product to the basket, the item disappears from the screen.
When returning to the collectionview, if the user wants to select the same previous item, the event is not triggered and the product fails to load, forcing the user to select any item, return to the query, and select the previous item again

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read src/Controls/src/Core/Items/SelectableItemsView.cs, starting at SelectionModePropertyChanged and the existing SelectionChanged flow. Trace how repeated taps on the same item are suppressed, then verify that the proposed ItemTapped behavior raises an event for every tap while existing selection modes remain unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
frontend, mobile-dev
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.