Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataGrid doesnt proper loads rows #108

Open
Xiaoy312 opened this issue Aug 14, 2020 · 1 comment
Open

DataGrid doesnt proper loads rows #108

Xiaoy312 opened this issue Aug 14, 2020 · 1 comment
Labels
project/input Categorizes an issue or PR as relevant to input (Button, CheckBox, ...)

Comments

@Xiaoy312
Copy link

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));
@scara1701
Copy link

scara1701 commented Sep 5, 2020

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

@agneszitte agneszitte added the project/input Categorizes an issue or PR as relevant to input (Button, CheckBox, ...) label Sep 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
project/input Categorizes an issue or PR as relevant to input (Button, CheckBox, ...)
Projects
None yet
Development

No branches or pull requests

3 participants