Display of WPF DataGrid multi-thread update data

Source: Internet
Author: User
Tags wpf datagrid

Loading the data source of the DataGrid requires a lot of Io operations. It cannot be displayed on the UI only after all the data is read. Since you are not familiar with WPF data binding, you do not have much time to study observecollection and other content. You can only use some clever methods.

To set the data source of the DataGrid, you only need to modify the itemssource attribute, as shown below:

List dataList = new List()datagrid1.ItemsSource = dataList;

However, if no data binding is performed, adding datalist will not update the UI. Unless you click the column title to sort the columns, the UI thread will update the data display. The active method is to use the refresh method as follows:

dataGrid1.Items.Refresh();

I have seen some articles saying that using updatelayout is not very useful. This solves the problem of UI update.

Loading data sources requires another thread. in WPF, non-UI threads are not allowed to perform direct operations on controls in the UI thread. This is the same for Android, it should be from the security perspective. However, if you really need to update the UI space, it is not unsolved. Handler can be used in Android, while dispatcher of the control is used in WPF. The begininvoke method of the dispatcher must be called. See an article (http://msdn.microsoft.com/en-us/library/757y83z4 (V = vs.100) on msdn ). aspx), we will talk about using the begininvoke method to update the UI control in multiple threads. However, this article is about Windows form, which is different from WPF, begininvoke is the control method. Begininvoke can be viewed as calling a callback method, so it uses the delegate.

The following is a sample code (for WPF ):

        //...        //new Thread(ThreadMethod).Start();        private void ThreadMethod()        {            object[] param = { 1000 };            dataGrid1.Dispatcher.BeginInvoke(new updateDateGridDelegate(UpdateDateGrid), param);        }        private delegate void updateDateGridDelegate(int time);        private void UpdateDateGrid(int time)        {            Thread.Sleep(time);            dataGrid1.Items.Refresh();            return;        }

Of course, it is better to study data binding. Because only a small part of the job has a limited time, this non-mainstream approach is used.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.