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>();
相关推荐
迪飞特科技21 小时前
分布式事务下 Spring 声明式事务失效的 3 种修复方案
分布式·spring·wpf
2601_962174173 天前
Redis 简介
数据库·redis·wpf
SamChan905 天前
Python+PyMuPDF提取PDF表格并翻译:表格结构检测与双语重建实战
windows·python·ai·pdf·wpf
SamChan905 天前
Python+OpenCV检测PDF翻译后排版偏移:像素级格式一致性验证
python·opencv·ai·pdf·wpf
FuckPatience5 天前
WPF 拿到控件的最初样式,修改获得焦点样式
wpf
主宰者5 天前
【WPF】MainWindow前添加加载窗口,发现加载过程中有白色无内容窗口闪烁一下的解决方案
wpf
Vae_Mars6 天前
WPF中的ControlTemplate(控件模板)
wpf
SamChan906 天前
Python+ReportLab自动生成PDF翻译质量审计报告:从数据到可视化的完整方案
开发语言·python·ai·pdf·wpf
circuitsosk6 天前
多智能体协同在能源数据分析中的实践:分配-执行-校验闭环架构设计
python·数据分析·wpf·能源·并行计算·多智能体系统·agent协作
weixin199701080167 天前
《大促护航:电商API限流与降级,双11峰值500万调用的架构复盘》(附Python源码)
python·架构·wpf