为什么当的ItemsSource改变DataGrid中不更新? [英] Why does the DataGrid not update when the ItemsSource is changed?

查看:846
本文介绍了为什么当的ItemsSource改变DataGrid中不更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的WPF应用程序在DataGrid,我有一个简单的问题。我有一个通用的清单,我想每一个对象被添加到集合时间这个集合绑定到我的数据网格的数据源。而我没有兴趣使用观察的集合。

I have a datagrid in my wpf application and I have a simple problem. I have a generic list and I want to bind this collection to my datagrid data source every time an object is being added to the collection. and I'm not interested to use observable collection.

的一点是,我用同样的方法在其他地方,并且工作正常。 。但是当我按下这个时候添加按钮的对象添加和DataGrid更新正确,但在第二个项目加入到收集数据网格已经不更新

the point is I'm using the same method somewhere else and that works fine. but this time when i press Add button an object is added and datagrid updates correctly but from the second item added to collection datagrid does not update anymore.

下面是代码:

 private void btnAddItem_Click(object sender, RoutedEventArgs e)
    {
        OrderDetailObjects.Add(new OrderDetailObject
        {
            Price = currentitem.Price.Value,
            Quantity = int.Parse(txtQuantity.Text),
            Title = currentitem.DisplayName,
            TotalPrice = currentitem.Price.Value * int.Parse(txtQuantity.Text)
        });

        dgOrderDetail.ItemsSource = OrderDetailObjects;
        dgOrderDetail.UpdateLayout();
    }



任何想法?

any idea ?

推荐答案

的ItemsSource 总是相同的,到您的收藏,没有什么变化,没有更新的参考。你可以之前空出来:

The ItemsSource is always the same, a reference to your collection, no change, no update. You could null it out before:

dgOrderDetail.ItemsSource = null;
dgOrderDetail.ItemsSource = OrderDetailObjects;



另外,您也可以只刷新项目:

Alternatively you could also just refresh the Items:

dgOrderDetail.ItemsSource = OrderDetailObjects; //Preferably do this somewhere else, not in the add method.
dgOrderDetail.Items.Refresh();



我不认为你真的想叫 UpdateLayout请有...

拒绝使用一个ObservableCollection不太好主意的)

这篇关于为什么当的ItemsSource改变DataGrid中不更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆