gpt4 book ai didi

c# - WPF DataGrid 删除 IEditableCollectionView.CancelNew() 上的 NewItemPlaceholder

转载 作者:太空狗 更新时间:2023-10-29 21:53:47 27 4
gpt4 key购买 nike

概览

我正在开发一个 WPF 应用程序(使用 .NET 4.5),其中一部分涉及在 DataGrid 中显示一些数据。
用户可以在 DataGrid 中添加新行并通过别处的按钮删除一行。

当用户开始添加无法提交的新行然后按下删除按钮时,我遇到了一个问题。
应取消新行并将 DataGrid 重置为其先前的状态。
但是,DataGrid 的 NewItemPlaceholder 行被删除并且再也不会显示。

我做了一个 sample project这说明了这个问题。
Here也是一个简短的截屏视频。
This是示例应用的样子。

重现:

  1. 双击最上面一行的“价格”单元格
  2. 输入无效数字以通过异常触发验证
  3. (可选)选择另一行
  4. 点击删除按钮

代码

View 模型获取 ObservableCollection 中的数据,该数据用作 Collection View 的源。我有一个连接到删除按钮的简单命令。如果用户正在添加项目 (IEditableCollectionView.IsAddingNew),我尝试在 collectionView 上使用 .CancelNew() 取消操作。但是,当命令完成时,DataGrid 将删除其 NewItemPlaceholder

到目前为止,我已经尝试过:

  • Triggering the DataGrid通过设置 dataGrid.CanUserAddRows = true 使占位符再次出现,这在一定程度上解决了这个问题,但这是一个可怕的解决方法,它有错误,之后占位符不可编辑。
  • 从源集合中删除无效项目:this.productsObservable.Remove(this.Products.CurrentAddItem as Product)
    这不会改变行为,占位符仍被删除。
  • 从 Collection View 中删除项目:this.Products.Remove(this.Products.CurrentAddItem)
    这会引发异常,这是有道理的:在 AddNew 或 EditItem 事务期间不允许“删除”。

还有其他方法可以取消用户的添加并显示 NewItemPlaceholder 吗?

在示例项目中,为了简单起见,我在 VM 构造函数中实例化了数据。
实际上,我正在使用对服务的异步调用,将结果包装在 ObservableCollection 中,ViewModel 实现 INotifyPropertyChanged。业务对象未实现 INPC。

示例项目的 XAML:

<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication3"
Title="MainWindow" Height="250">
<Window.DataContext>
<local:ViewModel />
</Window.DataContext>

<StackPanel Orientation="Vertical">
<Button Command="{Binding DeleteCommand}" Content="Delete row" />
<DataGrid
ItemsSource="{Binding Products}"
CanUserDeleteRows="False"
CanUserAddRows="True"
SelectionMode="Single">
</DataGrid>
</StackPanel>
</Window>

ViewModel,以及一个简单的业务对象:

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows.Data;
using System.Windows.Input;

namespace WpfApplication3
{
public class ViewModel
{
private readonly ObservableCollection<Product> productsObservable;

public ViewModel()
{
this.productsObservable = new ObservableCollection<Product>()
{
new Product() { Name = "White chocolate", Price = 1},
new Product() { Name = "Black chocolate", Price = 2},
new Product() { Name = "Hot chocolate", Price = 3},
};

this.Products = CollectionViewSource.GetDefaultView(this.productsObservable) as IEditableCollectionView;
this.Products.NewItemPlaceholderPosition = NewItemPlaceholderPosition.AtBeginning;

this.DeleteCommand = new DelegateCommand(this.OnDeleteCommandExecuted);
}

public ICommand DeleteCommand { get; private set; }
public IEditableCollectionView Products { get; private set; }

private void OnDeleteCommandExecuted()
{
if (this.Products.IsAddingNew)
{
this.Products.CancelNew();
}
}

}

public class Product
{
public string Name { get; set; }
public decimal Price { get; set; }
}
}

最佳答案

这个怎么样:

private void OnDeleteCommandExecuted()
{
if (this.Products.IsAddingNew)
{
this.Products.CancelNew();
this.Products.AddNew();
}
}

您仍然会删除输入错误的行,但会添加一个新的(大部分)空行。唯一的问题是您在数字列中得到默认的 0 值,而不是 null

关于c# - WPF DataGrid 删除 IEditableCollectionView.CancelNew() 上的 NewItemPlaceholder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29829818/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com