unoplatform / Uno.WindowsCommunityToolkit

The Windows Community Toolkit is a collection of helper functions, custom controls, and app services. It simplifies and demonstrates common developer tasks building UWP apps for Windows 10. The toolkit is part of the .NET Foundation.
https://docs.microsoft.com/windows/uwpcommunitytoolkit/
Other
61 stars 11 forks source link

DataGrid doesnt proper loads rows #108

Open Xiaoy312 opened 4 years ago

Xiaoy312 commented 4 years ago

Describe the bug

Setting the DataGrid.ItemsSource to an enumerable/collection doesnt show the rows of items. Only the columns are generated, if AutoGenerateColumns has not been set to false.

Steps to Reproduce

Steps to reproduce the behavior:

Expected behavior

The data to be displayed.

Screenshots

n/a

Environment

NuGet Package(s):

<PackageReference Include="Uno.Microsoft.Toolkit.Uwp.UI.Controls" Version="6.1.0-build.186.g928c99f74d" />
<PackageReference Include="Uno.Microsoft.Toolkit.Uwp.UI.Controls.DataGrid" Version="6.1.0-build.186.g928c99f74d" />
Windows 10 Build Number:
- [ ] Fall Creators Update (16299)
- [ ] April 2018 Update (17134)
- [ ] October 2018 Update (17763)
- [ ] May 2019 Update (18362)
- [ ] Insider Build (build number: )

App min and target version:
- [ ] Fall Creators Update (16299)
- [ ] April 2018 Update (17134)
- [ ] October 2018 Update (17763)
- [ ] May 2019 Update (18362)
- [ ] Insider Build (xxxxx)

Device form factor:
- [x] macOS
- [x] WASM
- [ ] iOS (?: not tested)
- [ ] Android (?: not tested)
- [ ] UWP

Visual Studio 
- [ ] 2017 (version: )
- [x] 2019 (version: ) 
- [ ] 2019 Preview (version: )

Additional context

note: If you attach the assignment to a button handler. Tapping a first time will make the columns to be generated, and tapping it again will cause the rows to be shown.

workaround:

public static class DataGridExtensions
{
    #region Property: DelayedItemsSource

    public static DependencyProperty DelayedItemsSourceProperty { get; } = DependencyProperty.RegisterAttached(
        "DelayedItemsSource",
        typeof(IEnumerable),
        typeof(DataGridExtensions),
        new PropertyMetadata(default, (d, e) => d.Maybe<DataGrid>(control => OnDelayedItemsSourceChanged(control, e))));

    public static IEnumerable GetDelayedItemsSource(DataGrid obj) => (IEnumerable)obj.GetValue(DelayedItemsSourceProperty);
    public static void SetDelayedItemsSource(DataGrid obj, IEnumerable value) => obj.SetValue(DelayedItemsSourceProperty, value);

    #endregion

    private static void OnDelayedItemsSourceChanged(DataGrid sender, DependencyPropertyChangedEventArgs e)
    {
        // DataGrid not showing rows with initial values, only "late" provided values are displayed somehow...
        // This is the workaround for that:
        _ = RunOnUIThread(() => sender.ItemsSource = (IEnumerable)e.NewValue);
    }

    private static async Task RunOnUIThread(Action action)
    {
        await CoreApplication.MainView.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        {
            action();
        });
    }
}

for databinding, just bind to that attached property instead of ItemsSource. for code-behind, just call SetDelayedItemsSource during OnLoaded like so:

Task.Run(() => DataGridExtensions.SetDelayedItemsSource(dg, items));
scara1701 commented 4 years ago

I also have the same issue on Android. Columns are generated but rows are not displayed. View: https://github.com/scara1701/UnoWithMVVM/blob/master/UnoWithMVVM/UnoWithMVVM.Shared/Views/DetailsView.xaml

ViewModel that grid is bound to: https://github.com/scara1701/UnoWithMVVM/blob/master/UnoWithMVVM.Core/ViewModels/DetailsViewModel.cs

Behavour on UWP with Microsoft.Toolkit.Uwp.UI.Controls.DataGrid works as expected.

GitHub
scara1701/UnoWithMVVM
Lab of Uno platform combinded with new Microsoft.Toolkit.MVVM - scara1701/UnoWithMVVM
GitHub
scara1701/UnoWithMVVM
Lab of Uno platform combinded with new Microsoft.Toolkit.MVVM - scara1701/UnoWithMVVM