WPF Datagrid Binding Doesn't Update Until Clicking Row Header

后端 未结 2 1777
死守一世寂寞
死守一世寂寞 2021-01-08 00:49

I have a datagrid that is bound to a List in the view model. The grid\'s contents don\'t update until I click on the row header. Clicking in various cells doesn\'t affect it

2条回答
  •  北海茫月
    2021-01-08 01:11

    The problem is that when you add or remove an item from the collection, the setter isn't called. This means INotifyPropertyChanged isn't called, and the view has no way of knowing it needs to be refreshed.

    WPF solves this by also supporting the INotifyCollectionChanged interface.

    Try using an ObservableCollection instead of a List. ObservableCollection is built in, and implements INotifyCollectionChanged, so you won't have to do much to your code.

    Also make sure that you continue to implement INotifyPropertyChanged on your view model as you already have. This way the view will be notified when you replace the entire collection (when the setter is called).

    public ObservableCollection TransactionDetailList
    {
        get { return this._transactionDetailList; }
    
        set
        {
            this._transactionDetailList = value;
            RaisePropertyChanged("TransactionDetailList");                
        }
    }
    

    See:

    • http://msdn.microsoft.com/en-us/library/ms668604.aspx
    • http://msdn.microsoft.com/en-us/library/ms748365.aspx (although this isn't using MVVM style)

    Edit:

    Also, you should implement INotifyPropertyChanged on TransactionDetail too. If you can't, wrap it in a class that does implement INotifyPropertyChanged.

    If you don't implement it on TransactionDetail, than changes you make that don't impact the list, but do impact properties on a TransactionDetail instance, won't show in the UI until you somehow refresh the whole list.

    If you tried to fix this by calling RaisePropertyChanged on the list property, then your UI thinks the entire list (and hence the whole set of UI objects) needs to be thrown out and updated. This will kill your performance and make your app sluggish.

提交回复
热议问题