我怎么能在数据网格的WPF删除所选行? [英] How I can Delete Selected Row in datagrid wpf?
问题描述
我使用WPF数据网格我需要删除选定的行,我的代码是
I am using WPF datagrid I need to delete selected Row , my code is
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
操作无效而的ItemsSource正在使用中。访问和修改
元素与ItemsControl.ItemsSource而不是
Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead
我怎么能删除所选行?
推荐答案
您从来没有从电网WPF删除行。你要做的是什么:
You never have to delete the row from the WPF grid. What you have to do is:
1)定义与包含对象列表的的ObservableCollection
属性类型在网格上呈现的值。
1) define a type with a ObservableCollection
property that contains a list of objects presenting the values on your grid.
2)绑定的属性网格控制。
2) Bind that property to your grid control.
3)现在从绑定集合如果添加/删除对象,相应的行会分别添加/控制的用户界面中删除。
3) now if you add/remove objects from binded collection, corresponding rows will respectively add/remove from control's ui.
这篇关于我怎么能在数据网格的WPF删除所选行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!