Click here to Skip to main content
13,632,131 members
Rate this:
 
Please Sign up or sign in to vote.
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:

<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:

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 1-Jun-18 2:46am
Updated 13-Jul-18 14:29pm

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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).
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.180712.1 | Last Updated 13 Jul 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100