Click here to Skip to main content
15,916,398 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI,

i have a very slow ItemsControl with up to 5 DataGrids in it. (displays next to each other) Everytime when i update this Control my whole UI lags extremly (~10sec). Each Datagrid has ~50-400 items with predifined columns (not autogenerated). The itemsources of the grids get updated at the same time.

Here is my XAML:

XML
<pre>        <ScrollViewer Grid.Row="1">
            <ItemsControl ItemsSource="{Binding}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <UniformGrid Columns="{Binding Count}"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <DataGrid ItemsSource="{Binding Content}" RowHeight="20" AutoGenerateColumns="False" VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling">
                            <DataGrid.Columns>
                                <DataGridTextColumn Header="name" Binding="{Binding Name}" Width="100"  />
                                <DataGridTextColumn Header="value" Binding="{Binding Value}" Width="50" />
                            </DataGrid.Columns>
                        </DataGrid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
        </ScrollViewer>


And here the Code Behind:

C#
public partial class MainWindow : Window
   {

       public List<Folder> Folders { get; set; }

       public MainWindow()
       {
           InitializeComponent();

           Folders = new List<Folder>();
           Folders.Add(new Folder());
           Folders.Add(new Folder());

           this.DataContext = Folders;
       }

       private void Button_Click(object sender, RoutedEventArgs e)
       {
           Folders.ForEach(f => f.UpdateData());
       }
   }


   public class Folder
   {
       private  Random _rand = new Random();
       public ObservableCollection<Content> Content { get; set; }

       public Folder()
       {
           Content = new ObservableCollection<Content>();
       }

       public void UpdateData()
       {
           Content.Clear();
           for(int i = 1; i <= 300; i++)
               Content.Add(new Content($"C{i}", _rand.Next(1,400)));
       }
   }

   public class Content
   {
       public string Name  { get; set; }
       public int Value    { get; set; }

       public Content(string name, int value)
       {
           Name    = name;
           Value   = value;
       }
   }



Pls find also attached a full working example here.

Any ideas how i can speed up the datagrids without blocking the whole UI?

KR Manu

What I have tried:

I have tried to replace the datagrids with listboxes but the UI is still blocking.
Well it's not blocking that long but i need a solution without blocking at all.
Posted
Updated 13-Jul-18 14:29pm

1 solution

I am guessing that the slowdown occurs when you call the Update Data method in Folders?

If so, this is probably because you have linked the ObservableCollection as the ItemsSource to your grid. Each time you add something to this collection, the grid redraws (and you are adding 400 items to 5 grids = 2000 redraws).

You would find it quicker to disconnect your ItemsSource, update the collection and then reconnect the ItemsSource.

Alternatively, use a collection over which you have some control for the timing of notification (I created my own class for this purpose that handles a BlockNotifications / EnableNotifications).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900