簡體   English   中英

如何使用Delete鍵從DataGrid刪除行

[英]How to delete row from DataGrid with Delete key

我有一個DataGrid ,其中有一些記錄。 我想要的是,當我按鍵盤上的Delete鍵時,該行將被刪除,甚至收集中的記錄也將被刪除。

我以為參數CanUserDeleteRows可以解決問題,但不起作用。 當我按Delete鍵時,該行消失,但仍保留在集合中。

這是我的DataGrid

<DataGrid 
   Name="ProjectsGrid" 
   ItemsSource="{Binding Path=FilesCollection}" 
   AutoGenerateColumns="False" 
   CanUserAddRows="False"  
   CanUserDeleteRows="True">
   <DataGrid.Columns>
      <DataGridCheckBoxColumn Header="Use" Binding="{Binding Include}"/>
      <DataGridTextColumn Header="Name" Binding="{Binding Path, Converter={StaticResource PathConverter}}"/>
   </DataGrid.Columns>
</DataGrid>

這是我的ViewModel:

namespace Validator.ViewModels
{
  class SettingsVm : INotifyPropertyChanged
  {
    public void ChangeProperty(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

    public ObservableCollection<CsprojFile> FilesCollection { get; set;} 
    public SettingsVM()
    {
        FilesCollection = new ObservableCollection<CsprojFile>();
   }
}

我是否必須向我的ViewModel添加一些事件? 還是有其他我不知道的方式?

提前致謝。

如果要從View更新集合,則應將ItemSource的綁定模式提到為:

<DataGrid 
   Name="ProjectsGrid" 
   ItemsSource="{Binding Path=FilesCollection, Mode=TwoWay}" 
   AutoGenerateColumns="False" 
   CanUserAddRows="False"  
   CanUserDeleteRows="True">
            <DataGrid.Columns>
                <DataGridCheckBoxColumn Header="Use" Binding="{Binding Include}"/>
                <DataGridTextColumn Header="Name" Binding="{Binding Path}"/>
            </DataGrid.Columns>
        </DataGrid>

這應該可以解決您的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM