Wpf跨线程未显示DataGrid值

时间:2012-04-02 00:54:56

标签: c# wpf multithreading

这是我的代码:

    public void AddToGrid(string value)
    {
        MessageBox.Show(value);   //Message is showing with correct value
        dgrComponentList.Dispatcher.Invoke(new AddToGridDelegate(AddToGridSolid), System.Windows.Threading.DispatcherPriority.Normal, value);
    }
    private delegate void AddToGridDelegate(string value);
    private void AddToGridSolid(string value)
    {
        ((List<object>)this.dgrComponentList.ItemsSource).Add(new { ComponentName = value });
    }

当我从线程调用AddToGrid方法时,这不起作用。 但如果我打电话

,没有线程

((List<object>)this.dgrComponentList.ItemsSource).Add(new { ComponentName = value });

它的工作完美。我在哪里做错了?

由于

编辑:我刚刚发现,值存储在那里但不知何故它没有显示在网格中。

1 个答案:

答案 0 :(得分:2)

网格不知道集合中添加了新对象。实现更改的inotify集合或使用可观察集合来存储对象。