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>();
相关推荐
我好喜欢你~6 小时前
WPF---数据模版
wpf
hqwest1 天前
C#WPF实战出真汁07--【系统设置】--菜品类型设置
开发语言·c#·wpf·grid设计·stackpanel布局
hqwest2 天前
C#WPF实战出真汁08--【消费开单】--餐桌面板展示
c#·wpf·ui设计·wpf界面设计
orangapple2 天前
WPF 打印报告图片大小的自适应(含完整示例与详解)
c#·wpf
三千道应用题2 天前
WPF&C#超市管理系统(6)订单详情、顾客注册、商品销售排行查询和库存提示、LiveChat报表
开发语言·c#·wpf
✎ ﹏梦醒͜ღ҉繁华落℘3 天前
开发WPF项目时遇到的问题总结
wpf
hqwest4 天前
C#WPF实战出真汁06--【系统设置】--餐桌类型设置
c#·.net·wpf·布局·分页·命令·viewmodel
Vae_Mars4 天前
WPF中使用InputBindings进行快捷键绑定
wpf
hqwest4 天前
C#WPF实战出真汁05--左侧导航
开发语言·c#·wpf·主界面·窗体设计·视图viewmodel
hqwest5 天前
C#WPF实战出真汁01--项目介绍
开发语言·c#·wpf