unoplatform / uno

Open-source platform for building cross-platform native Mobile, Web, Desktop and Embedded apps quickly. Create rich, C#/XAML, single-codebase apps from any IDE. Hot Reload included! 90m+ NuGet Downloads!!
https://platform.uno
Apache License 2.0
8.94k stars 724 forks source link

ListView grouping only works with types that can be cast to IEnumerable<IEnumerable> #4293

Open baskren opened 4 years ago

baskren commented 4 years ago

Current behavior

ListView, provided with a nested IList<> source (via a CollectionViewSource), only provides a null DataContext to cell views.

Expected behavior

Same behavior as UWP: ListView provides cell views with a DataContext pulled from the objects given to ListView.ItemsSource.

How to reproduce it (as minimally and precisely as possible)

Small demo project: https://github.com/baskren/NestedCollectionListView

Steps to reproduce:

  1. clone demo project
  2. build and run UWP platform version: image Note: a. the three group header labels (Group 1, Group 2, Group 3) are white text on a black background. b. the contents of the eight (3 + 3 + 2) item cells (ex: Item 2:1 / Red) are black text (the title in bold) on a white background.
  3. build and run the Android platform version: image Note that the three group headers are rendered correctly but the eight item cell contents are all "NOT SET".
  4. build and run the WASM platform version: WASM result: image Note that it appears that the three group headers are not present and content of the eight item cells are all "NOT SET".

Note that, in the XAML for the group header (MyGroupHeader.xaml) and the item cell's (MyItemCell.xaml), the Text property for the TextBlocks are all pre-set to "NOT SET". In the code-behind for these views, a subscription to the DataContextChanged event is created in the constructor. The handler for this event updates the value of the Text property for these TextBlocks.

Workaround

Other things tried without success:

Environment

VisualStudio:

Microsoft Visual Studio Community 2019 Version 16.7.6 VisualStudio.16.Release/16.7.6+30611.23 Microsoft .NET Framework Version 4.8.04084

Installed Version: Community

Visual C++ 2019 00435-60000-00000-AA619 Microsoft Visual C++ 2019

ASP.NET and Web Tools 2019 16.7.532.28833 ASP.NET and Web Tools 2019

ASP.NET Core Razor Language Services 16.1.0.2035807+72d099b977d3a85e65fa3b0614ca8cfc803fef02 Provides languages services for ASP.NET Core Razor.

ASP.NET Web Frameworks and Tools 2019 16.7.532.28833 For additional information, visit https://www.asp.net/

Azure App Service Tools v3.0.0 16.7.532.28833 Azure App Service Tools v3.0.0

Azure Functions and Web Jobs Tools 16.7.532.28833 Azure Functions and Web Jobs Tools

C# Tools 3.7.0-6.20459.4+7ee7c540e3bf19765695b1331c946ddf986d1516 C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

CleanBinAndObjCommand Extension 1.1.1 CleanBinAndObjCommand Visual Studio Extension Detailed Info

Common Azure Tools 1.10 Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Extensibility Message Bus 1.2.6 (master@34d6af2) Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

IncrediBuild Build Acceleration 1.5.0.13 IncrediBuild effectively reduces compilation and development times by up to 90%.

IntelliCode Extension 1.0 IntelliCode Visual Studio Extension Detailed Info

Microsoft Azure Tools 2.9 Microsoft Azure Tools for Microsoft Visual Studio 2019 - v2.9.30701.1

Microsoft Continuous Delivery Tools for Visual Studio 0.4 Simplifying the configuration of Azure DevOps pipelines from within the Visual Studio IDE.

Microsoft JVM Debugger 1.0 Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Microsoft Library Manager 2.1.79+ge3567815aa.RR Install client-side libraries easily to any web project

Microsoft MI-Based Debugger 1.0 Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual C++ Wizards 1.0 Microsoft Visual C++ Wizards

Microsoft Visual Studio Tools for Containers 1.1 Develop, run, validate your ASP.NET Core applications in the target environment. F5 your application directly into a container with debugging, or CTRL + F5 to edit & refresh your app without having to rebuild the container.

Microsoft Visual Studio VC Package 1.0 Microsoft Visual Studio VC Package

Mono Debugging for Visual Studio 16.7.5 (112c7bc) Support for debugging Mono processes with Visual Studio.

Multilingual App Toolkit 4.0 Multilingual App Toolkit helps you localize your Windows Store app by providing file management, pseudo and machine translation, translation editor, and build integration. http://aka.ms/matinstall

NuGet Package Manager 5.7.0 NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

ProjectServicesPackage Extension 1.0 ProjectServicesPackage Visual Studio Extension Detailed Info

Rapid XAML Toolkit 0.11.0 Tools to accelerate XAML app development. This extension holds references and functionality used by the other Rapid XAML extensions.

SQL Server Data Tools 16.0.62008.03130 Microsoft SQL Server Data Tools

StylerPackage Extension 1.0 StylerPackage Visual Stuido Extension Detailed Info

Test Adapter for Boost.Test 1.0 Enables Visual Studio's testing tools with unit tests written for Boost.Test. The use terms and Third Party Notices are available in the extension installation directory.

Test Adapter for Google Test 1.0 Enables Visual Studio's testing tools with unit tests written for Google Test. The use terms and Third Party Notices are available in the extension installation directory.

TypeScript Tools 16.0.20702.2001 TypeScript Tools for Microsoft Visual Studio

UnoPlatformPackage Extension 1.0 UnoPlatformPackage Visual Studio Extension Detailed Info

UWP Essentials 1.4 The easiest way to set up Visual Studio for the ultimate UWP development experience.

Visual Basic Tools 3.7.0-6.20459.4+7ee7c540e3bf19765695b1331c946ddf986d1516 Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual C++ for Cross Platform Mobile Development (Android) 16.0.30309.115 Visual C++ for Cross Platform Mobile Development (Android)

Visual C++ for Cross Platform Mobile Development (iOS) 16.0.30309.115 Visual C++ for Cross Platform Mobile Development (iOS)

Visual F# Tools 10.10.0.0 for F# 4.7 16.7.0-beta.20361.3+3ef6f0b514198c0bfa6c2c09fefe41a740b024d5 Microsoft Visual F# Tools 10.10.0.0 for F# 4.7

Visual Studio Code Debug Adapter Host Package 1.0 Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

Visual Studio Container Tools Extensions (Preview) 1.0 View, manage, and diagnose containers within Visual Studio.

Visual Studio Tools for CMake 1.0 Visual Studio Tools for CMake

Visual Studio Tools for Containers 1.0 Visual Studio Tools for Containers

VisualStudio.DeviceLog 1.0 Information about my package

VisualStudio.Mac 1.0 Mac Extension for Visual Studio

Windows Template Studio 3.8.20273.1 Windows Template Studio quickly builds a UWP app, using a wizard-based UI to turn your needs into a foundation of Windows 10 patterns and best practices.

Xamarin 16.7.000.456 (d16-7-6@f8d55b6) Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer 16.7.0.495 (remotes/origin/d16-7@79c0c522c) Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin Templates 16.7.85 (1bcbbdf) Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

Xamarin.Android SDK 11.0.2.0 (d16-7/025fde9) Xamarin.Android Reference Assemblies and MSBuild support. Mono: 83105ba Java.Interop: xamarin/java.interop/d16-7@1f3388a ProGuard: Guardsquare/proguard/proguard6.2.2@ebe9000 SQLite: xamarin/sqlite/3.32.1@1a3276b Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-7@017078f

Xamarin.iOS and Xamarin.Mac SDK 14.0.0.0 (7ec3751a1) Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

Nuget Package:

Nuget Package Version(s):

UWP: (TargetPlatformVersion: 10.0.17763.0 TargetPlatformMinVersion: 10.0.17763.0) Microsoft.NETCore.UniversalWindowsPlatform Version=6.2.9 Uno.Core Version=2.0.0

Android: (TargetFrameworkVersion: 10.0) Uno.UI.RemoteControl Version=3.1.0 Uno.UniversalImageLoader Version=1.9.32 Microsoft.Extensions.Logging.Console Version=1.1.1 Microsoft.Extensions.Logging.Filter Version=1.1.1 Xamarin.AndroidX.Browser Version=1.0.0 Xamarin.AndroidX.Lifecycle.LiveData Version=2.1.0

WASM: (TargetFramework: netstandard2.0) Microsoft.Extensions.Logging.Console Version=1.1.1 Microsoft.Extensions.Logging.Filter Version=1.1.1 Uno.UI.WebAssembly Version=3.1.0 Uno.UI.RemoteControl Version=3.1.0 Uno.Wasm.Bootstrap Version=1.3.0 Uno.Wasm.Bootstrap.DevServer Version=1.3.0

Affected platform(s):

IDE:

Relevant plugins:

Anything else we need to know?

Have only tried in UWP, Android and WASM. Do not know if issue is present elsewhere.

davidjohnoliver commented 4 years ago

@baskren I took a look at your sample, and narrowed it down to the fact that Uno doesn't understand that the MyGroup type can be a list of groups.

As a workaround, I managed to get it working on Android (and iOS) by adding the following type:

    public class MyGroups : MyGroup, IEnumerable<MyGroup>
    {
        public MyGroups(string title) : base(title)
        {

        }
        IEnumerator<MyGroup> IEnumerable<MyGroup>.GetEnumerator() => (this as IList<IMyItem>).OfType<MyGroup>().GetEnumerator();
    }

and changing the source declaration to the following (note changed type in first line):

        MyGroup myGroup = new MyGroups("Big group")
        {
            new MyGroup("Group 1")
            {
                new MyItem<int>("Item 1:1") { Value = 10 },
                new MyItem<int>("Item 1:2") { Value = 20 },
                new MyItem<int>("Item 1:3") { Value = 30 }
            },
            new MyGroup("Group 2")
            {
                new MyItem<string>("Item 2:1") { Value="Red"},
                new MyItem<string>("Item 2:2") { Value="Green"},
                new MyItem<string>("Item 2:3") { Value="Blue"},
            },
            new MyGroup("Group 2")
            {
                new MyItem<bool>("Item 2:1") { Value=false},
                new MyItem<bool>("Item 2:2") { Value=true},
            },
        };

The basic idea here is that for the object set as CollectionViewSource.Source to be groupable, Uno expects a type that can be cast to IEnumerable<IEnumerable>. I've updated the bug description accordingly. Hopefully that unblocks you.

Note for contributors: The bug is here: https://github.com/unoplatform/uno/blob/0c9de5d418ab197928bda5a69c61670298a18652/src/Uno.UI/UI/Xaml/Data/CollectionView.cs#L270-L278

davidjohnoliver commented 4 years ago

Also to note: on WebAssembly, the group headers still won't show up because grouping isn't implemented yet for the WASM ListView. That issue is tracked under this epic, but feel welcome to open a specific issue for WASM ListView grouping if it's something important to you.

iosub commented 3 years ago

Hi I have udate uno to 3.4.0-dev.306 and add a refresh Button

`private void Refresh_Click(object sender, RoutedEventArgs e) {

        try
        {
            var cvs = new CollectionViewSource();

            cvs.IsSourceGrouped = true;
            cvs.Source = myGroup;
            _listView.ItemsSource = cvs.View;
        }
        catch (Exception ex)
        {

            var a = 1;
        }

    }

`

I get an error i Android and IOS

ava.Lang.IllegalStateException Message=View with id -1: crc64a5a37c43dff01024.Grid#onMeasure() did not set the measured dimension by calling setMeasuredDimension()

at Windows.UI.Xaml.Controls.ListViewBase.ClearContainerForDragDrop (Windows.UI.Xaml.UIElement itemContainer) [0x00000] in C:\a\1\s\src\Uno.UI\UI\Xaml\Controls\ListViewBase\ListViewBase.DragDrop.cs:95 at Windows.UI.Xaml.Controls.ListViewBase.ContainerClearedForItem (System.Object item, Windows.UI.Xaml.Controls.Primitives.SelectorItem itemContainer) [0x00000] in C:\a\1\s\src\Uno.UI\UI\Xaml\Controls\ListViewBase\ListViewBase.cs:669 at Windows.UI.Xaml.Controls.ItemsControl.CleanUpContainer (Windows.UI.Xaml.DependencyObject element) [0x0003d] in C:\a\1\s\src\Uno.UI\UI\Xaml\Controls\ItemsControl\ItemsControl.cs:974 at Windows.UI.Xaml.Controls.VirtualizingPanelLayout.RemoveAllViews () [0x00049] in C:\a\1\s\src\Uno.UI\UI\Xaml\Controls\ListViewBase\VirtualizingPanelLayout.Android.cs:377 at Windows.UI.Xaml.Controls.VirtualizingPanelLayout.Refresh () [0x00000] in C:\a\1\s\src\Uno.UI\UI\Xaml\Controls\ListViewBase\VirtualizingPanelLayout.Android.cs:621 at Windows.UI.Xaml.Controls.NativeListViewBase.Refresh () [0x0002b] in C:\a\1\s\src\Uno.UI\UI\Xaml\Controls\ListViewBase\NativeListViewBase.Android.cs:293 at Windows.UI.Xaml.Controls.ListViewBase.Refresh () [0x00006] in C:\a\1\s\src\Uno.UI\UI\Xaml\Controls\ListViewBase\ListViewBase.Android.cs:126 at Windows.UI.Xaml.Controls.Primitives.Selector.OnItemsSourceChanged (Windows.UI.Xaml.DependencyPropertyChangedEventArgs e) [0x0000d] in C:\a\1\s\src\Uno.UI\UI\Xaml\Controls\Primitives\Selector.cs:308 at Windows.UI.Xaml.Controls.ItemsControl+<>c.<.cctor>b__194_4 (Windows.UI.Xaml.DependencyObject s, Windows.UI.Xaml.DependencyPropertyChangedEventArgs e) [0x00000] in C:\a\1\s\src\Uno.UI\UI\Xaml\Controls\ItemsControl\ItemsControl.cs:240 at Windows.UI.Xaml.PropertyMetadata.RaisePropertyChanged (Windows.UI.Xaml.DependencyObject source, Windows.UI.Xaml.DependencyPropertyChangedEventArgs e) [0x00000] in C:\a\1\s\src\Uno.UI\UI\Xaml\PropertyMetadata.cs:187 at Windows.UI.Xaml.DependencyObjectStore.InvokeCallbacks (Windows.UI.Xaml.DependencyObject actualInstanceAlias, Windows.UI.Xaml.DependencyProperty property, Windows.UI.Xaml.DependencyPropertyDetails propertyDetails, System.Object previousValue, Windows.UI.Xaml.DependencyPropertyValuePrecedences previousPrecedence, System.Object newValue, Windows.UI.Xaml.DependencyPropertyValuePrecedences newPrecedence, System.Boolean bypassesPropagation) [0x000e3] in C:\a\1\s\src\Uno.UI\UI\Xaml\DependencyObjectStore.cs:1552 at Windows.UI.Xaml.DependencyObjectStore.RaiseCallbacks (Windows.UI.Xaml.DependencyObject actualInstanceAlias, Windows.UI.Xaml.DependencyPropertyDetails propertyDetails, System.Object previousValue, Windows.UI.Xaml.DependencyPropertyValuePrecedences previousPrecedence, System.Object newValue, Windows.UI.Xaml.DependencyPropertyValuePrecedences newPrecedence) [0x0005b] in C:\a\1\s\src\Uno.UI\UI\Xaml\DependencyObjectStore.cs:1461 at Windows.UI.Xaml.DependencyObjectStore.InnerSetValue (Windows.UI.Xaml.DependencyProperty property, System.Object value, Windows.UI.Xaml.DependencyPropertyValuePrecedences precedence, Windows.UI.Xaml.DependencyPropertyDetails propertyDetails) [0x00195] in C:\a\1\s\src\Uno.UI\UI\Xaml\DependencyObjectStore.cs:478 at Windows.UI.Xaml.DependencyObjectStore.SetValue (Windows.UI.Xaml.DependencyProperty property, System.Object value, Windows.UI.Xaml.DependencyPropertyValuePrecedences precedence, Windows.UI.Xaml.DependencyPropertyDetails propertyDetails) [0x0002e] in C:\a\1\s\src\Uno.UI\UI\Xaml\DependencyObjectStore.cs:417 at Windows.UI.Xaml.DependencyObjectStore.SetValue (Windows.UI.Xaml.DependencyProperty property, System.Object value) [0x00000] in C:\a\1\s\src\Uno.UI\UI\Xaml\DependencyObjectStore.cs:381 at Uno.UI.Controls.BindableView.SetValue (Windows.UI.Xaml.DependencyProperty dp, System.Object value) [0x00000] in C:\a\1\s\src\Uno.UI\obj\Release\MonoAndroid10.0\g\DependencyObjectGenerator\Uno_UI_Controls_BindableView_eefac468be1af0c618115e95339af76c.g.cs:47 at Windows.UI.Xaml.Controls.ItemsControl.set_ItemsSource (System.Object value) [0x00000] in C:\a\1\s\src\Uno.UI\UI\Xaml\Controls\ItemsControl\ItemsControl.cs:232 at NestedIEnumerableListView.Shared.MainPage.Refresh_Click (System.Object sender, Windows.UI.Xaml.RoutedEventArgs e) [0x0001d] in C:\MSP\2018\Uno\bugs\NestedCollectionListView.git\NestedIEnumerableListView\NestedIEnumerableListView.Shared\MainPage.xaml.cs:66