简体   繁体   English

如何允许用户仅删除DataGrid中的新行

[英]How to allow user to delete only new rows in datagrid

I am using a DataGrid to show data to the user. 我正在使用DataGrid向用户显示数据。

I am allowing the user to edit the data and add new rows. 我允许用户编辑数据并添加新行。 Is it possbile to use the built-in funcionality of CanUserDeleteRows to allow the user only to delete rows that he just added. 是否可以使用CanUserDeleteRows的内置功能允许用户仅删除刚添加的行。

So usecase-description: 所以用例描述:

1) User opens datagrid 2) datagrid shows its contents 3) user adds a few rows 4) user deletes one (or all) of his added rows. 1)用户打开datagrid 2)datagrid显示其内容3)用户添加几行4)用户删除他添加的行中的一个(或全部)。 5) user is not able to delete rows that were in the datagrid when it initally loaded 5)初始加载时,用户无法删除datagrid中的行

...so only "new" rows should be able to get deleted. ...因此,只有“新”行才能被删除。 What approach is best here? 哪种方法最好? I am a little bit out of ideas at the moment... 目前我有点想法...

UPDATE: To satisfty your comments ;) 更新:满足您的意见;)

I am using an ObservableCollection of an custom class to fill the datagrid. 我正在使用自定义类的ObservableCollection来填充datagrid。 So the "problem" is that I don't actually know ho to "intercept" the delete-event nor how to do it without breaking the mvvm-pattern. 因此,“问题”是我实际上不知道如何“拦截”删除事件,也不知道如何在不破坏mvvm模式的情况下做到这一点。

You have two choices : 您有两种选择:

1 `Disconnected Mode`

You save your new rows on cache, You use GetChanges method in order to get added rows. 您将新行保存在缓存中,使用GetChanges方法以获取添加的行。

//GetChanges return added rows before update datasource from caching
var newRows = YourDataTable.GetChanges(DataRowState.Added);

You add another Data Column, you set value of this column every time that you add new rows. 您添加另一个数据列,每次添加新行时都要设置此列的值。

DataColumn columnFlag = new DataColumn("FlagDelete", typeof(bool));

Delete Button is visible by according with column value 通过按列值visible Delete Button

2 `Connected Mode` 

You create in your database new column, you realize server calls in order to get your datas, and visualize your function delete according to the value of column flag 您在数据库中创建新列,实现服务器调用以获取数据,并根据列标志的值可视化函数删除

As I am using ObservableCollections and so on, I used a working approach, a little bit based on Aghilas Yakoub's answer. 当我使用ObservableCollections等时,我使用了一种可行的方法,该方法有点基于Aghilas Yakoub的回答。

I added a Remove-Button to the DataGrid thats visibility is bound to a IsNew -Property. 我向DataGrid添加了一个Remove-Button,它的可见性绑定到IsNew -Property。

<DataGridTemplateColumn>
   <DataGridTemplateColumn.CellTemplate>
      <DataTemplate>
          <Button Content="Remove" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabControl}}, Path=DataContext.BottomDetailVM.DeleteCommand}" Visibility="{Binding IsNew, Converter={StaticResource Bool2VisibilityConverter}, FallbackValue=Collapsed}" CommandParameter="{Binding}" />
      </DataTemplate>
   </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

When the usere clicks the button the command is executed with the current object (that is shown in the row) as a parameter. 当用户单击按钮时,命令将以当前对象(在行中显示)作为参数执行。

public ICommand DeleteRangfolgeCommand
{
  get
  {
     return new ActionCommand<MyOwnViewModel>(ExecuteDelete);
  }
}

private void ExecuteDelete(MyOwnViewModel viewModelToDelete)
{
    this.ItemsSourceList.Remove(viewModelToDelete);
}

Btw: I am using the RelayCommand -implementation (in my case called ActionCommand) from the MSDN-Magazine . 顺便说一句:我正在使用MSDN-Magazine中RelayCommand实现(在我的情况下称为ActionCommand)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM