wpf 如何删除datagrid wpf中的选定行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26506901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How I can Delete Selected Row in datagrid wpf?
提问by Mahmoud Kassem
I am using WPF datagrid I need to delete selected Row , my code is
我正在使用 WPF 数据网格我需要删除选定的行,我的代码是
private void dataGridView1_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete)
{
this.dataGridView1.Items.Remove(this.dataGridView1.SelectedItem);
}
}
But when I using this code show me error
但是当我使用此代码时显示错误
Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead
使用 ItemsSource 时操作无效。使用 ItemsControl.ItemsSource 访问和修改元素
How I can Delete Selected Row ?
如何删除选定的行?
回答by Tigran
You never have to delete the row from the WPF grid. What you have to do is:
您永远不必从 WPF 网格中删除该行。你需要做的是:
1) define a type with a ObservableCollectionproperty that contains a list of objects presenting the values on your grid.
1) 定义一个带有ObservableCollection属性的类型,该属性包含在网格上呈现值的对象列表。
2) Bind that property to your grid control.
2) 将该属性绑定到您的网格控件。
3) now if you add/remove objects from binded collection, corresponding rows will respectively add/remove from control's ui.
3) 现在,如果您从绑定集合中添加/删除对象,相应的行将分别从控件的 ui 中添加/删除。
回答by Brad
I think you are using a itemSource to populate the dataGridview. Remove the item from the datasource and then refresh the binding.
我认为您正在使用 itemSource 来填充 dataGridview。从数据源中删除项目,然后刷新绑定。
Or have your datasource class inherit from INotifyPropertyChangedand raise a PropertyChangedevent and on the listbox XAML set the UpdateSourceTrigger as the PropertyChangedevent, such as below:
或者让您的数据源类继承INotifyPropertyChanged并引发PropertyChanged事件,并在列表框 XAML 上将 UpdateSourceTrigger 设置为PropertyChanged事件,如下所示:
ItemsSource="{Binding MyListItems, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}
回答by Dennis Kassel
Is guess your DataGrid is bound to an ItemsSource (e.g. an ObservableCollection). In that case manipulating the ItemsSource from the View is not allowed and you rather have to remove it in the ViewModel (that is where your bound objects are stored).
是猜测您的 DataGrid 绑定到 ItemsSource(例如 ObservableCollection)。在这种情况下,不允许从 View 操作 ItemsSource,而您必须在 ViewModel(即存储绑定对象的位置)中将其删除。
回答by Davide Piras
As clearly mentioned in the error description for a UI control bound to a DataSource you should be manipulating the data source itself and not the UI control ( in this case your data grid ).
正如绑定到 DataSource 的 UI 控件的错误描述中明确提到的,您应该操作数据源本身而不是 UI 控件(在这种情况下是您的数据网格)。
The UI Control is only a way to present your data in the User Interface, to show edited or new or modified data ( for example 1 row less ) you should simply act on the underlying data source you have assigned to the DataGrid's ItemSource property.
UI 控件只是在用户界面中显示数据的一种方式,要显示已编辑或新的或已修改的数据(例如少 1 行),您应该简单地对分配给 DataGrid 的 ItemSource 属性的基础数据源进行操作。

