虽然线程已完成,但datagrid的ItemsSource并不令人耳目一新

时间:2012-03-29 18:58:41

标签: wpf multithreading datagrid itemssource

我有一个wpf应用程序。 它搜索一些网站的东西。 它此时冻结了。 所以我在另一个线程中进行搜索事件。 但在搜索线程时,我没有得到datagrid的行。 我的错在哪里,有什么建议吗?

这是我的代码:

ClsInternet.cs
using System.Collections.ObjectModel;
using System.Windows.Controls;
using System.Threading;
using System.Windows;
using System;
using System.Windows.Media;

namespace WpfApplication1
{
    public class ClsInternet
    {

        public void searchSomething(string param, ClsSearch pobj, DataGrid dg)
        {
            Thread t = new Thread(() =>
            {
                ObservableCollection<SeachResult> lstGrid = null;
                try
                {
                    MainWindow.isProcessing = true;
                    lstGrid = new ObservableCollection<SeachResult>();
                    lstGrid.Add(new SeachResult { StrAddress = "www.abc.com", StrContent = "some content from abc" });
                    lstGrid.Add(new SeachResult { StrAddress = "www.def.com", StrContent = "some content from def" });

                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message, "Seach Error!", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                finally
                {
                    MainWindow.isProcessing = false;
                    pobj.LstSearchResult = lstGrid;


                    dg.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate()
                    {
                        dg.ItemsSource = pobj.LstSearchResult;
                        if (dg.ItemsSource != null)
                        {
                            foreach (SeachResult ozellik in dg.ItemsSource)
                            {
                                DataGridRow satir = dg.ItemContainerGenerator.ContainerFromItem(ozellik) as DataGridRow;
                                if (ozellik.StrContent.Contains("def"))
                                    satir.Background = Brushes.Firebrick;
                            }
                        }
                    }));
                }               
            });
            //
            t.IsBackground = true;
            if (!MainWindow.isProcessing)
                t.Start();
        }
    }
}



ClsSearch.cs
using System;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace WpfApplication1
{
    public class ClsSearch : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(info));
        }

        private ObservableCollection<SeachResult> _lstSearchResult;
        public ObservableCollection<SeachResult> LstSearchResult
        {
            get
            {
                return _lstSearchResult;
            }
            set
            {
                _lstSearchResult = value;
                NotifyPropertyChanged("LstSearchResult");
            }
        }
    }

    public class SeachResult
    {
        private string _strAddress;
        public string StrAddress
        {
            get { return _strAddress; }
            set { _strAddress = value; }
        }

        private string _strContent;
        public string StrContent
        {
            get { return _strContent; }
            set { _strContent = value; }
        }
    }
}



MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Button Name="btnSearch" Width="100" Height="25" Content="CLICK" Click="btnSearch_Click" Grid.Row="0"></Button>
        <DataGrid Name="dgResult" Grid.Row="1"
                  ItemsSource="{Binding Path=LstSearchResult, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></DataGrid>
    </Grid>
</Window>




MainWindow.xaml.cs
using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        ClsSearch objSearch;
        public static bool isProcessing;

        public MainWindow()
        {
            InitializeComponent();
            isProcessing = false;
            objSearch = new ClsSearch();
            dgResult.DataContext = objSearch;
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            ClsInternet objClsInternet = new ClsInternet();
            objClsInternet.searchSomething("", objSearch, dgResult);
        }
    }
}

提前感谢。

0 个答案:

没有答案