wpf datagrid 删除行的两种方式

csharp 复制代码
<DataGrid
   AutoGenerateColumns="False"
   CanUserAddRows="False"
   Grid.Row="1"
   KeyDown="DataGrid_OnKeyDown"
   PreviewKeyDown="DataGrid_OnPreviewKeyDown"
   RowHeight="28"
   SelectedCellsChanged="DataGrid_OnSelectedCellsChanged"
   SelectionUnit="CellOrRowHeader"
   x:Name="DataGrid">

   <DataGrid.Columns>
       <DataGridTextColumn
           Binding="{Binding Value}"
           Header="文件"
           Width="*">
           <DataGridTextColumn.ElementStyle>
               <Style TargetType="TextBlock">
                   <Setter Property="VerticalAlignment" Value="Center" />
               </Style>
           </DataGridTextColumn.ElementStyle>

           <DataGridTextColumn.EditingElementStyle>
               <Style TargetType="TextBox">
                   <Setter Property="VerticalContentAlignment" Value="Center" />
               </Style>
           </DataGridTextColumn.EditingElementStyle>
       </DataGridTextColumn>
       <!--  ElementStyle="{StaticResource CenteredTextBlockStyle}"  -->
   </DataGrid.Columns>
</DataGrid>

由于列使用的是 嵌套了textblock组件的型式 编辑的时候会变成textbox组件

所以这里使用 PreviewKeyDown 来绑定 delete键 来删除行

两种删除方式的区别在于 SelectionUnit 的类型

第一种当 SelectionUnit="CellOrRowHeader"的时候

csharp 复制代码
private void DataGrid_OnPreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Delete)
    {
        // 获取所有行
        var items = DataGrid.Items;
        
        // 获取选中的单元格
        var selectedCells = DataGrid.SelectedCells;
        
        // 使用 HashSet 来避免重复行
        var selectedRows = new HashSet<Bds>();
        
        foreach (var cell in selectedCells)
        {
            if (cell.Item is Bds item)
            {
                selectedRows.Add(item);
            }
        }
        
        // 从数据源中删除选中的项
        foreach (var item in selectedRows)
        {
            _gridFileList.Remove(item);
        }
        e.Handled = true;
    }
}

第二种 当SelectionUnit="FullRow" 的时候

csharp 复制代码
private void DataGrid_OnPreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Delete)
    {
         确保有选中的行
         if (DataGrid.SelectedItems.Count > 0)
         {
             // 获取选中的项
             var selectedItems = DataGrid.SelectedItems.Cast<Bds>().ToList();
        
             // 从数据源中删除选中的项
             foreach (var item in selectedItems)
             {
                 // 假设你有一个 ObservableCollection 或 List 作为数据源
                 _gridFileList.Remove(item);
             }
        
             // 取消事件,以防止其他处理
             
             
             e.Handled = true;
         }
        
    }
}

附上数据类型

csharp 复制代码
public class Bds : NotificationObject
{
    private string _name;

    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            RaisePropertyChanged();
        }
    }

    private string _value;

    public string Value
    {
        get { return _value; }
        set
        {
            _value = value;
            RaisePropertyChanged();
        }
    }

    private string _tishi;

    public string Tishi
    {
        get { return _tishi; }
        set
        {
            _tishi = value;
            RaisePropertyChanged();
        }
    }

    private string _fileName;

    public string FileName
    {
        get { return _fileName; }
        set
        {
            _fileName = value;
            RaisePropertyChanged();
        }
    }

    private string _exportFile;

    public string ExportFile
    {
        get { return _exportFile; }
        set
        {
            _exportFile = value;
            RaisePropertyChanged();
        }
    }
}

附上数据源

csharp 复制代码
ObservableCollection<Bds> _gridFileList = new ObservableCollection<Bds>();
相关推荐
大霸王龙5 小时前
系统模块与功能设计框架
人工智能·wpf
明耀20 小时前
WPF DataGrid 默认显示行号
wpf
lph19721 天前
wpf的converter
wpf
fyifei05581 天前
WPF学习PropertyChanged
wpf
爱炸薯条的小朋友1 天前
C#由于获取WPF窗口名称造成的异常报错问题
windows·c#·wpf
baivfhpwxf20231 天前
wpf ListBox 去除item 单击样式
wpf
诗仙&李白1 天前
lnnovationHubTool,用prism+WPF编写的MVVM模式的快速上位机软件开发框架平台
wpf·mvvm·prism·上位机软件开发框架平台
程序员小刘1 天前
【HarmonyOS 5】教育开发实践详解以及详细代码案例
华为·wpf·harmonyos
Java Fans2 天前
在WPF项目中集成Python:Python.NET深度实战指南
python·.net·wpf
布伦鸽2 天前
C# WPF 左右布局实现学习笔记(1)
笔记·学习·c#·wpf