取消删除操作 - NotifyCollectionChangedAction

发布于 2024-09-13 02:46:31 字数 1338 浏览 10 评论 0原文

我在视图模型中使用以下代码从集合中删除项目:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

粉红×色少女 2024-09-20 02:46:31

那么,您无法在 CollectionChanged 事件期间取消删除操作。

我的建议:如果您使用 MVVM,则应该在某个地方有一个删除命令,当在 DataGrid 中按下删除键时会触发该命令。在此命令的 Execute() 方法中,您应该:

  1. 请求确认。
  2. 如果用户选择“是”,则从集合中删除该项目。此删除应直接反映在 DataGrid 上。
  3. 如果用户选择否,则不执行任何操作。

这意味着,尽管 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:

  1. Ask the confirmation.
  2. If user chooses yes, then remove the item from the collection. This removal should directly be reflected on the DataGrid.
  3. If user chooses no, do nothing.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文