Reputation: 133
When I remove an item from an ObservableCollection
that has invalid data in it the datagrid
won't clear the fact that it has errors so once I delete it it acts like the DataGrid
still has errors and won't allow me to edit/add and edit data.
I'm using MVVM so I can't just do datagrid.refresh
:\
Any ideas?
Upvotes: 2
Views: 1406
Reputation: 1
After removing item (that have validation error) from ObservableCollection, recreate ObservableCollection and raise OnPropertyChanged.
This refresh DataGrid, rows you are created before delete remains and are editable because validation error of removed item/row is gone.
Like this:
public ObservableCollection<Person> Persons { get; private set; }
...
private void DeleteRowCommand_Method()
{
Persons.Remove(SelectedPerson);
Persons = new ObservableCollection<Person>(Persons);
OnPropertyChanged("Persons");
}
...
Upvotes: 0
Reputation: 133
I came up with this using Phil's answer:
protected override void RemoveItem(int index)
{
this[index] = new EngineStatusUserFilter();
base.RemoveItem(index);
Refresh();
}
public void Refresh() {
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); }
I set the old object to a new one before removing it so it will be valid.
Upvotes: 1
Reputation: 42991
I don't know if this will work, but you can try telling the data grid the entire collection has changed:
Two options:
1) Raise a property change notification for the collection property.
public class MyViewModel : ViewModelBase
{
private void RefreshItems()
{
RaisePropertyChanged("Items");
}
private ObservableCollection<DataItem> Items { ... }
}
2) Derive from ObservableCollection so that you can raise a NotifyCollectionChanged event
public class MyCollection : ObservableCollection<DataItem>
{
public void Refresh()
{
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}
Upvotes: 2