取消删除操作 - NotifyCollectionChangedAction
我在视图模型中使用以下代码从集合中删除项目:
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);
}
}
}
}
}
我有一个绑定到集合的数据网格。我想知道是否有办法根据确认提示取消删除操作?我注意到 NotifyCollectionChangedEventArgs 没有取消方法。发生的情况是,当用户从数据网格中删除某个项目但在确认时选择“否”时,该项目仍会从数据网格中删除。它不会从数据库中删除,如果刷新数据网格,它将再次出现。我正在使用 mvvm 模式,并且我更喜欢这样做而无需编写数据网格代码。任何帮助表示赞赏。
I am using the following code in my viewmodel to delete items out of 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 that is bound to the collection. I am wondering if there is anyway of canceling the remove action based on the confirmation prompt? I noticed NotifyCollectionChangedEventArgs does not have a cancel method. What happens is when a user deletes an item out of the datagrid but chooses 'no' on the confirmation, the item is still removed from the datagrid. It isn't deleted from the database and if the datagrid is refreshed it will appear again. I am using the mvvm pattern and I prefer to do this without having to code my datagrid. Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么,您无法在 CollectionChanged 事件期间取消删除操作。
我的建议:如果您使用 MVVM,则应该在某个地方有一个删除命令,当在 DataGrid 中按下删除键时会触发该命令。在此命令的 Execute() 方法中,您应该:
这意味着,尽管 DataGrid.CanUserDeleteRows 设置为 False,因为您基本上必须控制何时删除行。
希望这有帮助。
Well, you can't cancel a remove action during a CollectionChanged event.
My suggestion: if you're using MVVM, you should have a DeleteCommand somewhere that is triggered when the DeleteKey is pressed in the DataGrid. In the Execute() method of this command, you should:
This means, though that the DataGrid.CanUserDeleteRows is set to False since you basically have to control when the rows get deleted.
Hope this helps.