0

I have a WPF project with a DataGrid that is bound to an Observable Collection. This collection is populated using an async method, when I call this async method an animation with progress ring is shown to the user.

While the collection is waiting for the data everything works without delays, but when the DataGrid is populating itself with the items, the progress ring animation stops working for 1 second or 2 until the DataGrid loads all records (This Collection may have 50,000 records or more). I was just wondering if there is a way to not lock the animation when the DataGrid is populating itself with the information returned from the collection.

The setup is: I have a main Window with a A hidden user control that shows/hides the animation using a a property named IsLoading. When this User Control is visible it locks all user objects behind it.

The Code is

<Grid>
 <local:LoadingOverlayView 
        DataContext="{Binding Assets}"
        d:IsHidden ="True"
        Visibility="{Binding IsLoading, 
        Converter={StaticResource BoolToVisibilityConverter},
                UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
J_PT
  • 479
  • 4
  • 22
  • 1
    Did you try using a different Thread for your Animation? – Master117 Dec 22 '16 at 14:59
  • Can you give a sample how to do that? – J_PT Dec 22 '16 at 15:14
  • You need to use [BackGroundWorker](https://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx) ,also go through this [How to use WPF Background Worker](http://stackoverflow.com/questions/5483565/how-to-use-wpf-background-worker) – Tk1993 Feb 08 '17 at 12:10

2 Answers2

0

Best way to implement animated progress rings, bars, etc. is to use secondary thread for them. Later to synchronize other threads with ui thread use Dispatcher.Invoke() method.

mustf4
  • 105
  • 3
  • Dispatcher.Invoke() is a method used to tell the Dispatcher to execute something (like a delegate) synchronously on the thread the Dispatcher is associated with. It's not used to "_synchronize other threads with ui thread_". – Alberto Solano Dec 22 '16 at 15:38
0

This means virtualisation of the datagrid is turned off. It's creating all the UI element for all rows. This can have different causes. Most likely because you put the datagrid in a scrollpanel. By doing this it doesn't know how height it is and cant determine the minimal set of rows it has to create UI elements for.

this is good read: https://msdn.microsoft.com/en-us/library/cc716879(v=vs.110).aspx but it can become pretty complex

Dave Smits
  • 1,871
  • 14
  • 22
  • Certainly the virtualization of rows and columns could help, but it has its own limits as well. I would recommend to use virtualization together with a background thread, in order not to block the UI thread, especially in the case there are several rows to be displayed. – Alberto Solano Dec 22 '16 at 15:33
  • I really doubt that; the question states clearly: "While the collection is waiting for the data everything works without delays, but when the DataGrid is populating itself with the items, the progress ring animation stops working for 1 second or 2 until the DataGrid loads all records (This Collection may have 50,000 records or more). " So loads without delay and the process ring animation stops; indicates he is not having its delay building the collection but at the moment the datagrid is going to do its work. – Dave Smits Dec 22 '16 at 16:23
  • Exactly, is when the DataGrid is populating itself that the Animation of the progress Ring blocks a short period of time... This is not a big deal but it would be better if I could continue to see the ring animation without "choking" itself while the DataGrid is loading the Data... I also not sure how to isolate this in 2 different threads since these are 2 different WPF UserControls bound to a common True/False property that shows or hides the Animation after the load of the Datagrid completes.... – J_PT Dec 22 '16 at 18:04
  • In the DataGRid I have EnableColumnVirtualization="True" EnableRowVirtualization="True" – J_PT Dec 22 '16 at 18:06
  • By using the xaml live visual tree you can see how many rows are created. This show be small number. It's hard to understand for use to see why virtualization is not working. There are so many factors involved. Best is read the blog I provided – Dave Smits Dec 22 '16 at 18:21
  • 39 DataGridRow are shown in Live Visual Tree of 20.000 Items, and as I scroll new DataGridRows are created and the others are deleted. Th Ui itself shows 36, the 3 extra should be buffer. – J_PT Dec 22 '16 at 18:27
  • Virtualization works then... you will need to profile then – Dave Smits Dec 22 '16 at 18:29