An ObservableCollection
will notify you of a change. There is no reason to do this manually, so you can delete all OnPropertyChanged("JobEntities");
. This will lead you to a cleaner solution.
MSDN
WPF provides the ObservableCollection class, which is a built-in data collection implementation that implements the INotifyCollectionChanged Interface.
The next part is that the ObservableCollection will notify you of changes to the collection itself (add / remove). Any changes to the item in the list will not be submitted. For this, the simplest method is to implement the INotifyPropertyChanged
elements used in the Observable Collection.
I am using PRISM 5 in this example, so it should be very similar to what you are doing. There are a couple of major design changes you code. First, I use the direct property for my Observable Collection. We know that the structure will handle any add / remove operations to this collection. Then, to notify me when I change a property inside an object in an observable collection, I used the notify property in the TestEntity
class TestEntity
.
public class MainWindowViewModel : BindableBase {
Here is my essence, pay attention to BindableBase
and the fact that I am notifying about the change. This allows the DataGrid
or whatever you use to be notified of a property change.
public class TestEntity : BindableBase { private String _name; public String Name { get { return _name; } set { SetProperty(ref _name, value); } }
Now actually all TestEntity
should have implemented INotifyPropertyChanged
for this, but I am using PRISM BindableBase
as an example.
EDIT
I found a similar question about SO. I think yours is a little different, but they overlap with concepts. This may help to view it.
Observed collection Notify when a property is changed in MVVM
EDIT
If the datagrid is sorted, the previous method will not update the grid. To deal with this, you need to update the grid view, but you cannot access it directly using MVVM. Therefore, to handle this, you will want to use CollectionViewSource
.
public class MainWindowViewModel : BindableBase {
The TestEntity
class TestEntity
not change, but here again the class:
public class TestEntity : BindableBase { private String _name; public String Name { get { return _name; } set { SetProperty(ref _name, value); } }
For clarification, here my XAML shows the binding to the new CollectionViewSource
.
<DataGrid Grid.Row="1" ItemsSource="{Binding ViewSource.View}"></DataGrid>
For further reading, you can refer to the MSDN article.
Here's another topical question / answer - Re-sort WPF DataGrid after limited data has changed