简体   繁体   中英

updating datagrid with BindingList

I've been looking at the difference between a BindingList and an observablecollection and List. From what I've read, it seems like the BindingList is the only collection type that will notify if an object in it has one of its properties changed. I cannot get this to work.

I have a property on a ViewModel called Matches, which returns a BindingList created out of a list of CarMatch objects in another class. (Cars m_Cars = new Cars();) My DataGrid on the View is bound to this Matches property in the VM.

public BindingList<CarMatch> Matches
{ 
    get
    {
        Return new BindingList<CarMatch>(m_Cars.Matches);  
    }
}

Now, in the code I change one of the CarMatch object's properties, say.. automaticTrans = true from false. Matches[0].automaticTrans = true. I want to see that change in the DataGrid. Without implementing INotifyPropertyChanged inside of the CarMatch class, is there a way to update the datagrid from the viewmodel? Using INotifyPropertyChanged on Matches does not seem to do it. There is something about this I just don't understand, and could use an example to look at.

CarMatch (not Matches ) has to implement INotifyPropertyChanged . But consider using ObservableCollection unless you really need some of the additional scenarios offered by BindingList : with ObservableCollection , INotifyPropertyChanged comes for free. And, more importantly, BindingList doesn't scale well .

try

dataGrid.Items.Refresh();

but keep in mind that is a expensive call if you have lots of data and you call it several times in a short period of time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM