Datagrid : Expanding a column after a total row deletion crashs the Datagrid
- Dominant language
- C#
- Stars
- 7.7k
- Forks
- 1.3k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 61
Description
### Description
[DataGrid_VisualCrash SIMPLE.zip](https://github.com/dotnet/wpf/files/13841344/DataGrid_VisualCrash.SIMPLE.zip)
A. After a total deletion of all the rows present in Datagrid, the column resizing crash on visual.cs
Complementary/Very similars bugs :
B. After 50 element added then all deleted, it crash on visual.cs
C. (Same project) A scrollviewer Visible without any row in the Datagrid trigger a crash on visual.cs
### Reproduction Steps
A. Column resize bug
1. Click on 'Add 10'
2. Click on 'Erase All'
3. Resize the column width with mouse
4. See error "The specified Visual is not an ancestor of this Visual"
B. 50 elements total deletion bug
1. Click on 'ADD 10' (5 times, so 50elements)
2. Click on 'Erase All'
3. See error "The specified Visual is not an ancestor of this Visual"
C. Scrollviewer visibility bug
1. In XAML, set the Scrollviewer visibility to "Visible"
2. Launch the project
3. See error "The specified Visual is not an ancestor of this Visual"
### Expected behavior
No crash
### Actual behavior
System.InvalidOperationException: "The specified Visual is not an ancestor of this Visual"
Source : PresentationCore
Then the application is crashing.
File : Visual.cs
Method : TrySimpleTransformToAncestor
### Regression?
_No response_
### Known Workarounds
For the A we constraint the Datagrid CanUserResizeColumn to a flag "HasAtLeastOneRow".
For the C we constraint the Scrollviewer Visibility to a flag "HasAtLeastOneRow" with a visibility converter.
For the B there isn't workaround.
### Impact
A more problematic issue which is happening on datagrid on a more complex C# Solution selled to our client. Sometimes, after a MassDeletion, this issue appears, generating many tickets that we cannot solve.
### Configuration
Tested on
- .NET Framework 4.7.2, .NET 7 (A, B and C)
- .NET 8 for A & C (B seems to be fixed)
- Windows 10 and 11
### Other information

[DataGrid_VisualCrash SIMPLE.zip](https://github.com/dotnet/wpf/files/13841344/DataGrid_VisualCrash.SIMPLE.zip)
Contributor guide
Assessment
This issue has not been assessed yet.