wpf icon indicating copy to clipboard operation
wpf copied to clipboard

When Popup (combobox, tooltip, menu item, etc) is shown within an app with a DataGrid, the DataGrid becomes slow and less responsive.

Open brswan opened this issue 1 year ago • 4 comments

Description

When Popup (combobox, tooltip, menu item, etc) is shown within an app with a DataGrid, the DataGrid becomes slow and less responsive. When debugging this problem, we output properties from before and after the popup was shown. It appears the datagrid has several properties that get changed when only a popup is shown. Properties of interest include SizeToContentV, HasStarCellsU, HasGroup3CellsInAutoRows.

Screenshot 2024-10-02 134404

I ran a performance profiler and can see that after the popup is shown the Layout time increases by a major factor as well as the frame rate drops substantially.

Screenshot 2024-10-02 142645

I have tried to fix the datagrid column width and row height to no avail. I have also overridden the cell template with fixed size values and this did not work either.

datagrid-02

DataGridTest.zip

Reproduction Steps

  • Create an app with a combobox and datagrid (see attachment).
  • Run the app and vertically scroll. Scrolling will be smooth without lag.
  • Open the combobox to show the items panel. You do not need to select an item - the panel just needs to be shown
  • Close combobox panel
  • Vertically scroll the datagrid and you will see lag (it may be slight lag but it is an indicator of the problem)

Apps with more complex UIs will display this issue more apparently.

Included app has a button Export that will export all properties of the app. You can export before following the steps above. Then export again once slowness occurs and you will see properties are changed from before and after combobox items are displayed. Please see screen shot from Description for comparison.

Expected behavior

Datagrid should behave the same way before a Popup (combobox, tooltip, menu item, etc) is shown and after.

Actual behavior

Datagrid is responsive before a Popup (combobox, tooltip, menu item, etc) is shown. After the popup is shown, datagrid slows.

Regression?

Problem exists in NET6 and NET8

Known Workarounds

None at this time

Impact

Datagrid in the app becomes slow and sluggish to use. Our app is a real-time app that requires the datagrid to be responsive to user input.

Configuration

Windows 11 Pro 23H2 OS Build 22631.4169 Visual Studio 2022 Version 17.9.6 x64

Other information

No response

brswan avatar Oct 02 '24 18:10 brswan

This is because of #5807, GridView for example had one more speciality (#9181) but DataGrid just compensates it with actually having a lot of controls you need to have peers for.

h3xds1nz avatar Oct 02 '24 20:10 h3xds1nz

@h3xds1nz Thanks for pointing this out. Do you know if Microsoft is addressing this?

brswan avatar Oct 02 '24 20:10 brswan

It appears I was able to solve my data grid sluggish issue by making my own DataGrid class and returning null in OnCreateAutomationPeer. I'm not sure what ramifications this may have but it appears to work,.

    public class MyDataGrid : DataGrid
    {
        public MyDataGrid()
        {
        }


        protected override AutomationPeer OnCreateAutomationPeer()
        {
            return null;
            //return base.OnCreateAutomationPeer();
        }
    }

brswan avatar Oct 02 '24 21:10 brswan

I found a solution that works for my problem. I had to override the Windows class. From there you override the OnCreateAutomationPeer() method.

 public class CustomWindowAutomationPeer : FrameworkElementAutomationPeer
 {
     public CustomWindowAutomationPeer(FrameworkElement owner) : base(owner) { }

     protected override string GetNameCore()
     {
         return "CustomWindowAutomationPeer";
     }

     protected override AutomationControlType GetAutomationControlTypeCore()
     {
         return AutomationControlType.Window;
     }

     protected override List<AutomationPeer> GetChildrenCore()
     {
         return new List<AutomationPeer>();
     }
 }

In your custom Window class add

 protected override System.Windows.Automation.Peers.AutomationPeer OnCreateAutomationPeer()
   {
       return new CustomWindowAutomationPeer(this);
   }

brswan avatar Oct 03 '24 15:10 brswan

I am also experiencing this issue.

Specifically having a PopUp on the screen greatly hurts the performance of my datagrid with roughly 900-1200 rows and maybe 40 columns. It runs well until the PopUp first appears.

Tiptup300 avatar Feb 17 '25 14:02 Tiptup300

I encountered the same issue with ListView: the initial response is fast, but after any control pops up, the response delay increases sharply and becomes irreversible. Even after running for a long time, it does not recover.

Referring to the above, overriding the OnCreateAutomationPeer method can solve this problem.

I don’t know why such a performance issue occurs, nor do I understand whether overriding this method might lead to new problems.

eric-yr-yang avatar May 07 '25 05:05 eric-yr-yang