I use the following code in my view model to remove items from a collection:
UnitMeasureCollection.CollectionChanged += new NotifyCollectionChangedEventHandler(ListOfUnitMeasureCollectionChanged);
void ListOfUnitMeasureCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Remove)
{
if (NavigationActions.DeleteConfirmation("Delete Item.", "Are you sure you want to delete this item? This action cannot be undone."))
{
foreach (UnitMeasureBO item in e.OldItems)
{
UnitMeasureBO unitMeasureBO = item as UnitMeasureBO;
bool inUse = unitMeasureRepository.UnitMeasureInUse(unitMeasureBO.UnitMeasureValue);
if (inUse == true)
{
NavigationActions.ShowError("Cannot delete item", "This item cannot be deleted because it is used elsewhere in the application.");
}
else
{
unitMeasureRepository.DeleteUnitMeasure(unitMeasureBO.UnitMeasureValue);
}
}
}
}
}
I have a datagrid associated with a collection. I am wondering if in any case the deletion action is canceled based on the confirmation request? I noticed that NotifyCollectionChangedEventArgs does not have a cancellation method. What happens when a user deletes an item from a datagrid but selects no in the confirmation, the item is still removed from the datagrid. It is not deleted from the database and if the updated datagrid is updated, it will reappear. I use the mvvm template and I prefer to do this without the need for my datagrid code. Any help is appreciated.