question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[DataGrid]: using RowDetailsTemplate sets wrong DataContext

See original GitHub issue

Describe the bug

Hello,

When using a RowDetailsTemplate of a DataGrid, the RowDetailsTemplate will have the parent DataContext set, which causes a crash.

To Reproduce

  • Create a list of items
  • Assign to a DataGrid
  • Launch => the items are displayed.
  • Add a RowDetailsTemplate that display some property
  • Launch => crash

The issue also happens if there is no item in the list.

(example project included)

Expected behavior

The RowDetailsTemplate DataContext should be the Item and not the parent. If the list is empty RowDetailsTemplate should not be called since there is no item.

Example Project

DataGridBug.zip

In the example project if you launch with the RowDetailsTemplate commented no problem. But if you uncomment the RowDetailsTemplate you will get a crash because the wrong data context was set: Unable to cast object of type 'DataGridBug.ViewModels.ItemListViewModel' to type 'DataGridBug.ViewModels.ItemViewModel'.

Even worse if you use the ContentControl instead of the TextBlock it will recursively show the wrong View causing a StackOverFlow.

Informations

  • Avalonia Version 11.0.0

Edit: updated post

Issue Analytics

  • State:open
  • Created 2 months ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Gundzcommented, Jul 12, 2023

Hello,

I spent some time digging in the Avalonia Source code today and i think i found the issue and how to to fix it.

First i reproduced the issue in the ControlCatalog.

i Added a RowDetailsTemplate to the first Tab in the DataGridPage.

DataGridPage.xaml

//we are in the dataGrid1, after line 39
<DataGrid.RowDetailsTemplate>
    <DataTemplate>
        <ContentControl Content="{ReflectionBinding .}" />
    </DataTemplate>
</DataGrid.RowDetailsTemplate>

then i removed the ItemsSource assignation to set an empty list

DataGridPage.xaml.cs line 39

//dg1.ItemsSource = collectionView1;
dg1.ItemsSource = new List<Country>();

Now when launching and going to the DataGridPage we are getting the StackOverflow instead of getting an empty DataGrid.

After some debuging and digging, i found out that the problems comes from here

https://github.com/AvaloniaUI/Avalonia/blob/194044692eb3967b8c6bd0ed140a954f53b48e0e/src/Avalonia.Controls.DataGrid/DataGridRows.cs#L2973-L2976

In our case the dataItem is null, but the detailsContent.DataContext is set to ControlCatalog.Pages.DataGridPage

so when it enters the function bellow detailsContent.Measure i think it’s measuring the wrong stuff and goes on a recursive loop.

If i comment the check the problem is solved.

//if (dataItem != null)
{
    detailsContent.DataContext = dataItem;
}

Now if i relaunch the ControlCatalog and go to the DataGridPage:

  • When the list is empty, the grid is empty.
  • When there an item in the list it will print in the RowDetailsTemplate ControlCatalog.Models.Country like it should.
  • If i set the RowDetailsVisibilityMode to Visible, also no problem, it prints ControlCatalog.Models.Country for every item.

So:

  • when the dataitem is not null detailsContent.DataContext is set to the right item.
  • if the dataItem is null, the detailsContent.DataContext is set to null, and it’s not measuring the wrong thing at some point in the underlying measure function.

I didn’t investigate further in the underlying function, so maybe i’m wrong and/or there is another deeper issue.

I hope it helps. Thanks.

0reactions
Gundzcommented, Jul 13, 2023
Read more comments on GitHub >

github_iconTop Results From Across the Web

DataGrid SelectedItem Being Bound to Wrong DataContext ...
However, it appears as if the SelectedItem property is not correctly binding to the property I have set in my ViewModel. <Grid DataContext="{ ......
Read more >
How do I access the items of DataGrid in Datagrid row ...
I use C# WPF I want to display data in Nested DataGrid , like this ... DataContext = this; } public List<Employee> Employees...
Read more >
DataGridComboBoxColumn column displaying incorrect ...
I started with DataGridComboBoxColumn. I switched to Template columns, these have the same behavior. All the combobox ItemsSource are bound and that part ......
Read more >
[Solved] WPF nested Datagrid in datagrid details {binding}
Because DataGridComboBoxColumn doesn't inherit from FrameworkElement it is not rendered in the visual tree, so you can't use relative binding.
Read more >
Binding Properties of Controls in a RowDetailsTemplate
I'm trying to find out how to bind to something that is NOT in the DataContext, and is NOT a static resource. In...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found