AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
25.96k stars 2.25k forks source link

Update SelectionModel with IndexPath (as UWP) to use it with hierarchical list of items #12054

Open dbriard opened 1 year ago

dbriard commented 1 year ago

I run into a problem trying to make a grouped list view like this one: image

and finally I get something to work using a sample from UWP ItemsRepeater with Selection: https://github.com/microsoft/microsoft-ui-xaml/tree/main/dev/Repeater/TestUI/Samples/SelectionSample/Grouped

I know that Avalonia have a SelectionModel class, it it is not as complete as the one of UWP. The UWP SelectionModel can use a IndexPath object to track the selection. It allow SelectedIndex as int, but also (groupIndex, itemIndex) for 2-level hierarchy and a list of int for deeper level of hierarchy.

I used code from MordernWPF for SelectionModel, but that would be super useful if Avalonia's SelectionModel can be updated to use IndexPath.

For example, for the above list, I needed: SelectionModel.IsSelected(groupIndex, RepeatedIndex).Value

with index path looking like that in ModernWPF

public sealed class IndexPath
    {
        public IndexPath(int index)
        {
            m_path.Add(index);
        }

        public IndexPath(int groupIndex, int itemIndex)
        {
            m_path.Add(groupIndex);
            m_path.Add(itemIndex);
        }

        public IndexPath(IList<int> indices)
        {
            if (indices != null)
            {
                for (var i = 0; i < indices.Count; i++)
                {
                    m_path.Add(indices[i]);
                }
            }
        }
...
robloo commented 1 year ago

There were issues with SelectionModel supporting hierarchical selection. Therefore, that was removed. It was instead planned to add a separate TreeSelectionModel I believe.

This was discussed over a year ago and I would have to dig for the PR. It was with the ItemsControl or perhaps even the ItemsRepeater work.

Edit: Nevermind, I found it: https://github.com/AvaloniaUI/Avalonia/pull/4533#issuecomment-679849357