自学内容网 自学内容网

wpf datagrid 删除行的两种方式

<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"的时候

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” 的时候

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;
         }
        
    }
}

附上数据类型

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();
        }
    }
}

附上数据源

ObservableCollection<Bds> _gridFileList = new ObservableCollection<Bds>();


原文地址:https://blog.csdn.net/u014018281/article/details/143018217

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!