本文介绍了设置新行Datagrid的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGrid显示一些数据库有很多列。

我想这样,当用户编辑一个新行时,会自动设置一些值。

窗体DataGrid很容易,因为有RowsAdded事件处理程序。
但是我怎么能处理这个与wpf DataGrid?

编辑:我的DataGrid在Xaml绑定到一个公共属性是一个ITable。当用户在ComboBox中选择一个表时,将使用相应的表更新该属性。
是有自动生成列,用户可以输入新行的方式是编辑最后一个空行(默认行为)。

I have a DataGrid showing some databases having quite some columns.
I would like that, when the user edit a new row, some values are set automatically.
With the windows form DataGrid that would be easy, since there's RowsAdded event handler.But how could i handle this with the wpf DataGrid ??
Edit : my DataGrid is bound in Xaml to a public property which is an ITable. When user select a table in a ComboBox, the property is updated with corresponding table.Yes there's autogenerating column, and the way the user can enter a new row is to edit the last blank row (default behaviour).

推荐答案

好吧,我想我得到了。

当DataTable绑定到DataGrid时,创建一个CollectionView为了看到它。你可以通过使用(静态/共享)CollectionView.GetDefaultView(ThePropertyThatIsBound)方法获得它。

因为它实现ICollectionChanged,你可以添加一个事件处理程序到CollectionChangedEvent。

Ok i think i got it.
When a DataTable is bound to a DataGrid, a CollectionView is created in order to see it. You can get it by using the (static/shared) CollectionView.GetDefaultView(ThePropertyThatIsBound) method.
Since it implements ICollectionChanged, you can add an event handler to the CollectionChangedEvent.

在CollectionChanged事件处理程序中,如果您有一个新项目(e.NewItems.Count> 0),您必须检查它对System.Windows.Data.CollectionView.NewItemPlaceholder如果它不是占位符,那么它是一个全新的项目,我可以设置所有默认值。

In the CollectionChanged event handler, if you have a new item (e.NewItems.Count>0) you must check it against System.Windows.Data.CollectionView.NewItemPlaceholder and if it is not a place holder, then it is a brand new item, for wich i can set all default values.

这篇关于设置新行Datagrid的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:16
查看更多