c#WPF ListView异步来自代码

时间:2018-05-26 07:28:45

标签: c# wpf listview backgroundworker

我有一个ListView,我需要它定期调用数据库并显示一小组数据...我希望只是在XAML页面后面的代码中执行它。

    private readonly BackgroundWorker mWorker = new BackgroundWorker();
    private object m_lock = new object();
    ObservableCollection<Request> mNames = new ObservableCollection<Request>();
    public SettingsControl()
    {
        InitializeComponent();
        //LoadData();
        BindingOperations.EnableCollectionSynchronization(mNames, m_lock);
        mWorker.DoWork += LoadData;
        mWorker.RunWorkerAsync();
    }




   public void LoadData(object sender, DoWorkEventArgs e)
        {
            while (true)
            {
                BindingOperations.EnableCollectionSynchronization(mNames, m_lock);
                var db = new Database();
                var pendinglist = db.ReturnPendingUser();
                mNames.Clear();
                foreach (var user in pendinglist)
                {
                    mNames.Add(user);
                }
                lvDataBinding.ItemsSource = mNames;
            }


            Thread.Sleep(2000);

        }

我尝试使用后台工作程序来调用数据库并重新加载数据表但没有运气。

请咨询

1 个答案:

答案 0 :(得分:0)

基本上是user1672994提到的 我需要放置lvDataBinding.ItemsSource = mNames;在默认构造函数上。