WPF DataGrid和多线程(WPF DataGrid and multithreading)

编程入门 行业动态 更新时间:2024-10-17 00:25:47
WPF DataGrid和多线程(WPF DataGrid and multithreading)

我有一个DataGrid,我填充DataView(使用DataContext)。 我试图在一个单独的线程中做到这一点,但UI仍然冻结。 我想在填充DataGrid时阻止UI冻结。

这是我到目前为止的代码:

private void btnOK_Click(object sender, RoutedEventArgs e) { GetFieldsBLL getFieldsBLL = new GetFieldsBLL(); DataView dv = getFieldsBLL.GetWholeView(ViewName); Task task = new Task(() => ucDataExtracViewControl.PopulateGrid(dv)); task.Start(); } public void PopulateGrid(DataView dv) { dgView.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(delegate{ dgView.Columns.Clear(); dgView.AutoGenerateColumns = false; foreach (DataColumn column in dv.Table.Columns) { var gridColumn = new DataGridTextColumn() { Header = column.ColumnName, Binding = new Binding("[" + column.ColumnName + "]") }; dgView.Columns.Add(gridColumn); } dgView.DataContext = dv; DataView = dv; })); }

提前致谢!

编辑:我重新创建列的原因是因为某些列名称的名称中有一个点。 例如,“作业编号”在使用绑定时不会产生任何数据。 在这里阅读更多内容: DataTable列名是什么,带有点,使它们不适合WPF的DataGrid控件? 不能对数据库进行更改。 -

I have a DataGrid that I fill with a DataView (using DataContext). I have tried to do that in a separate thread but the UI still freezes. I want to prevent the UI from freezing when populating the DataGrid.

Here is the code I made so far:

private void btnOK_Click(object sender, RoutedEventArgs e) { GetFieldsBLL getFieldsBLL = new GetFieldsBLL(); DataView dv = getFieldsBLL.GetWholeView(ViewName); Task task = new Task(() => ucDataExtracViewControl.PopulateGrid(dv)); task.Start(); } public void PopulateGrid(DataView dv) { dgView.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(delegate{ dgView.Columns.Clear(); dgView.AutoGenerateColumns = false; foreach (DataColumn column in dv.Table.Columns) { var gridColumn = new DataGridTextColumn() { Header = column.ColumnName, Binding = new Binding("[" + column.ColumnName + "]") }; dgView.Columns.Add(gridColumn); } dgView.DataContext = dv; DataView = dv; })); }

Thanks in advance!

Edit: The reason why I recreate the columns is because some of the column names have a dot in their name. For instance “Job No.” doesn’t yield any data when using binding. Read more here: What is it about DataTable Column Names with dots that makes them unsuitable for WPF's DataGrid control? It is not an option to make changes to the database. –

最满意答案

我在WPF DataGrid上做了很多工作,渲染是一个问题。 最后,渲染时间将减少到您将要显示的列数和行数。 当DataGrid呈现它必须绘制每个时,这意味着加载和测量内容的大小。

我发现通过设置固定的列宽和行高可以提高速度。 当与下面的DelayedDataGridTextColumn结合使用时,UI线程几乎没有阻塞,因为每个单元格都是单独呈现的,因此允许在具有更高优先级的UI线程上发生其他事情。

public class DelayedDataGridTextColumn : DataGridTextColumn { protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem) { var textBlock = new TextBlock(); textBlock.SetValue(FrameworkElement.StyleProperty, ElementStyle); Dispatcher.BeginInvoke( DispatcherPriority.Loaded, new Action<TextBlock>(x => x.SetBinding(TextBlock.TextProperty, Binding)), textBlock); return textBlock; } }

请注意,您可以调整DispatcherPriority以适应所需的渲染速度。 优先级越低,您获得的幕布效果越多。 优先级越高,渲染时处理的其他项目越少。

I've worked quite a lot with the WPF DataGrid, and rendering is a problem. In the end rendering time comes down to the amount of columns and rows that you will be showing. When the DataGrid renders it has to draw each one, which means loading and measuring the size of the content.

I've found that you can get some good speed improvements by setting fixed column widths and row heights. When used in combination with the DelayedDataGridTextColumn below, there is very little blockage of the UI thread, because each cell is rendered separately, and thus allows for other things to happen on the UI thread with a higher priority.

public class DelayedDataGridTextColumn : DataGridTextColumn { protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem) { var textBlock = new TextBlock(); textBlock.SetValue(FrameworkElement.StyleProperty, ElementStyle); Dispatcher.BeginInvoke( DispatcherPriority.Loaded, new Action<TextBlock>(x => x.SetBinding(TextBlock.TextProperty, Binding)), textBlock); return textBlock; } }

Note that you can tweak the DispatcherPriority to fit the rendering speed you want. The lower the priority the more of a curtain effect you get. The higher the priority the less other items will be handled while rendering.

更多推荐

本文发布于:2023-08-04 13:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1416019.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多线程   WPF   DataGrid   multithreading

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!