缓慢的数据网格有~400行 [英] Slow datagrid with ~400 rows

查看:64
本文介绍了缓慢的数据网格有~400行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,



我有一个非常慢的ItemsControl,其中包含最多5个DataGrids。 (显示彼此相邻)每当我更新此控件时,我的整个UI都会落后(~10秒)。每个Datagrid都有大约50-400个具有预定列的项(不是自动生成的)。网格的项目源同时更新。



这是我的XAML:



< pre lang =xml> < pre > < ScrollViewer < span class =code-attribute> Grid.Row = 1 >
< ItemsControl < span class =code-attribute> ItemsSource = {Binding} >
< ItemsControl.ItemsPanel >
< ItemsPanelTemplate >
< UniformGrid = {Binding Count} / >
< / ItemsPanelTemplate >
< / ItemsControl.ItemsPanel >
< ItemsControl.ItemTemplate >
< DataTemplate >
< DataGrid ItemsSource = {Binding Content} RowHeight = 20 AutoGenerateColumns = 错误 VirtualizingPanel.IsVirtualizing = True VirtualizingPanel.VirtualizationMode = 回收 >
< DataGrid.Columns >
< DataGridTextColumn 标题 = name Binding = {Binding Name} Width = 100 / >
< DataGridTextColumn 标题 = value 绑定 = {绑定值} 宽度 = 50 / >
< / DataGrid.Columns >
< / DataGrid >
< / DataTemplate >
< / ItemsControl.ItemTemplate >
< / ScrollViewer >





这里代码背后:



  public   partial   class  MainWindow:Window 
{

public List< Folder>文件夹{获取; 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 文件夹
{
private Random _rand = new Random();
public ObservableCollection< Content>内容{获取; set ; }

public 文件夹()
{
Content = new ObservableCollection< Content>();
}

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

public class 内容
{
public string 名称{获得; set ; }
public int 值{ get ; set ; }

public 内容(字符串名称, int value
{
Name = name;
Value = value ;
}
}







请找附一个完整的工作示例此处



任何想法如何我可以在不阻塞整个用户界面的情况下加速数据网格吗?



KR Manu



什么我试过了:



我试图用列表框替换datagrids,但UI仍在阻塞。

嗯,这是没有阻塞那么长但我需要一个没有阻塞的解决方案。

解决方案

C {i},_ =。Next( 1 400 )));
}
}

public class 内容
{
public string 名称{获得; set ; }
public int 值{ get ; set ; }

public 内容(字符串名称, int value
{
Name = name;
Value = value ;
}
}







请找附一个完整的工作示例此处



任何想法如何我可以在不阻塞整个用户界面的情况下加速数据网格吗?



KR Manu



什么我试过了:



我试图用列表框替换datagrids,但UI仍在阻塞。

嗯,这是没有阻塞那么久,但我需要一个没有阻塞的解决方案。


我猜你在文件夹中调用Update Data方法时会发生减速?



如果是这样,这可能是因为您已将ObservableCollection作为ItemsSource链接到网格。每次向此集合添加内容时,网格都会重绘(并且您要添加400个项目到5个网格= 2000次重绘)。



您会发现断开连接的速度更快您的ItemsSource,更新集合,然后重新连接ItemsSource。



或者,使用一个集合,您可以通过它控制通知的时间(我创建了自己的类为此目的,处理BlockNotifications / EnableNotifications。)


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.

解决方案

"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.


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


这篇关于缓慢的数据网格有~400行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆