Uno.WindowsCommunityToolkit icon indicating copy to clipboard operation
Uno.WindowsCommunityToolkit copied to clipboard

DataGrid doesnt proper loads rows

Open Xiaoy312 opened this issue 5 years ago • 1 comments

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:

  • Add a DataGrid to a page
  • Databind OR Set ItemsSource directly from code behind
  • Launch the app, and navigate to that page

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));

Xiaoy312 avatar Aug 14 '20 16:08 Xiaoy312

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
Lab of Uno platform combinded with new Microsoft.Toolkit.MVVM - scara1701/UnoWithMVVM
GitHub
Lab of Uno platform combinded with new Microsoft.Toolkit.MVVM - scara1701/UnoWithMVVM

scara1701 avatar Sep 05 '20 14:09 scara1701