all items in list view are being deleted instead of selected line

tim 285 Reputation points
2026-09-02T13:14:20.36+00:00

the following code will remove all the item in the viewlist, how do I only remove the SelectedObj

  

---------------------
        private async void LoadFilterAction()
        {
            try
            {
                var FilterFile = await FilePicker.PickAsync();

                if (FilterFile != null)
                {
                    _sourceCache.Clear();
                    // read JSON 
                    _sourceCache.AddOrUpdate(FilterListService.GetSTrees(File.ReadAllText(FilterFile.FullPath)));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("iOS Main Exception: {0}", ex);
            }
        }


-----------------


 public void OnDeleteCommandAction(object SelectedObj)
        {
            try
            {
              /*  if (Resource.Global.SaveOK)
                {
                    _removeOK = true;
                };*/

                if (SelectedObj is FilterList filterList)
                {
                    _sourceCache.Remove(_trees);
                }
                /*  this does nothing
                                if (SelectedObj is FilterList filterList)
                                {
                                    _sourceCache.Remove(filterList));
                                }
                */
		
                                //   var filterList = SelectedObj as FilterList;
                                //    _trees.Remove(filterList.ToString());

            }
            catch (Exception ex)
            {
                Console.WriteLine("iOS Delete Exception: {0}", ex);
            }
        }

        public ReactiveCommand<Unit, Unit> SortCommand { get; }

        private SourceCache<FilterList, string> _sourceCache = new SourceCache<FilterList, string>(x => x.SortField.ToLower());
        public ReadOnlyObservableCollection<FilterList> Trees => _trees;

        private readonly ReadOnlyObservableCollection<FilterList> _trees;
        private string _searchText;
        private string _searchLocationText;
        private string _scrolltoText;
        private string _selectedSectorFilter;
        private string _selectedContainerFilter;
        private string _sortBy;
        public ICommand OnEditCommand { get; set; }
        public ICommand OnDeleteCommand { get; set; }


        private readonly IDisposable _cleanUp;

Developer technologies | XAML
Developer technologies | XAML

A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.

0 comments No comments

Answer accepted by question author
Tony Thach (WICLOUD CORPORATION) 760 Reputation points Microsoft External Staff Moderator
2026-09-03T02:10:58.9566667+00:00

Hi  @tim , and thanks for posting your question. 

I reproduced the reported behavior in a minimal .NET MAUI project using a ListView, five sample items, and the same SourceCache<FilterList, string> delete logic.

The command parameter is arriving correctly. SelectedObj is successfully cast to FilterList, so CommandParameter="{Binding .}" is working as intended. No change to the ListView, command binding, or CommandParameter is required.

User's image

The problem is this line:

_sourceCache.Remove(_trees);

_trees represents the entire collection bound to the ListView. Passing it to Remove removes all items contained in that collection from _sourceCache. This is why clicking Delete on one row clears the entire list.

Reproduced behavior before the fix:

ListView empty after clicking Delete on one item User's image

Fix

Remove the FilterList item already obtained from the command parameter:

public void OnDeleteCommandAction(object SelectedObj)
{
    try
    {
        if (SelectedObj is FilterList filterList)
        {
            _sourceCache.Remove(filterList);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("iOS Delete Exception: {0}", ex);
    }
}

The minimal required change is:

// Before: removes all items in the displayed collection
_sourceCache.Remove(_trees);

// After: removes only the item passed to the command
_sourceCache.Remove(filterList);

After applying this change in the reproduction project, clicking Delete removed only the selected row while the remaining items stayed in the ListView.

Result after applying the fix ListView showing only the selected item removed User's image

Therefore, the behavior can be resolved within the code provided. The root cause is passing the entire _trees collection to Remove instead of passing the selected filterList item.

If this instruction is applicable to your situation, I would greatly appreciate it if you could follow the instruction here so others experiencing similar behavior can benefit from it as well.  

Was this answer helpful?

5 people found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. tim 285 Reputation points
    2026-09-03T13:39:36.5833333+00:00

    that works, thanks you.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.