删除WPF DataGrid中的行

 布丁可爱_997 发布于 2023-01-06 18:41

我有一个数据网格,其中删除图标为一列,更新图标为另一列.单击更新时,第一个单元格设置为焦点.

单击删除我想删除所选行,但我收到错误"当ItemsSource正在使用时操作无效.请改为使用ItemsControl.ItemsSource访问和修改元素." 使用以下代码:

XAML:


    
        

        
            
                
            
            
                
                    
                
            
        

        
            
                
            
            
                
                    
                
            
        
    

C#:

private void EventSetter_OnHandler(object sender, MouseButtonEventArgs e)
{
    object source = e.OriginalSource;
    if (source.GetType() == typeof(Image))
    {
        grdList.IsReadOnly = false;

        selectedRow = FindParent(sender as DependencyObject);

        if (((DataGridCell)sender).Column.Header.ToString().ToUpperInvariant() == "DELETE")
        {
            grdList.Items.Remove(selectedRow);
        }
        else
        {
            DataGridCellsPanel panel = FindVisualChild(selectedRow);

            DataGridCell dgc = panel.Children[0] as DataGridCell; 
            dgc.Focus();
            grdList.BeginEdit();

            e.Handled = true;
        }
    }
}

另外如何使用"删除"键添加删除功能以及单击删除单元格.

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有