我是 WPF 新手。我想从数据网格运行时删除行。当我尝试像这样删除行时
Datagrid.Items.Remove(eRow);
它给我一个错误“错误是:在使用 ItemsSource 时操作无效。改为使用 ItemsControl.ItemsSource 访问和修改元素。”
我在网上看到您可以使用 ObservationCollection 和 InotifyPropertyChangedEvent,但我不知道如何实现它。
我有这样的删除按钮
这是数据网格
<ctrls:RhinoDataGrid x:Name="dataGrid" Grid.Row="1" Margin="5" ItemsSource="{Binding Model.CurrentDataTable}"
Style="{StaticResource RhinoDataGridBaseStyle}" IsReadOnly="{Binding Model.IsLinkFile}"
SelectedValue="{Binding Model.CurrentDataRow}" SelectedValuePath="Row"
>
</ctrls:RhinoDataGrid>
请帮助我。谢谢。
最佳答案
您的 DataGrid 的 ItemsSource 绑定(bind)了 Model.CurrentDataTable。如果要删除一行,则必须删除该集合中的项目。但如果集合未实现 INotifyCollectionChanged,DataGrid 不会注意到该修改.
.NET 有一个内置的 ObservableCollection ,它实现了 INotifyCollectionChanged。如果您将此集合类型用于 CurrentDataTable,则当集合被修改时,DataGrid 将自行更新。
关于Wpf Datagrid 删除行问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10564699/