添加新行后,为什么我的绑定控件没有更新?
我正在使用 wpf 拖放数据绑定到数据集。我使用 BindingListCollectionView 的 AddNew 方法生成一个新的数据表行。我在新行上设置值并在 BindingListCollectionView 上调用 CommitNew。 我希望在绑定控件中看到我分配的值,但它们都是空白。如果我将更改保存到数据库,控件就会更新,但我希望用户在调用之前看到我分配的值TableAdapterManager 上的 UpdateAll。
背景:
我在与我的 wpf 应用程序分开的项目中创建了一个强类型数据集。我的 wpf 应用程序引用了数据集应用程序。我在 wpf 应用程序中添加了一个指向类型化数据集的对象数据源。我将字段/控件从数据源窗口拖到 wpf 设计器窗口。生成的 xaml 包括 Window.Resources 部分,其中 CollectionViewSource 准确绑定到我的数据集和数据表。此 CollectionViewSource 是我拖动到设计图面的控件的 DataContext。所有控件都使用 TwoWay 数据绑定。
当窗口加载时,我获取对 xaml 的 CollectionViewSource 的引用(使用 FindResource)。然后,我获取对 CollectionViewSource 的视图属性的引用并将其转换为 BindingListCollectionView。现在,我使用 BindingListCollectionView 的 AddNew 方法生成一个新行(AddNew 作为对象返回)。我将该对象转换为 DataRowView 并访问它的 Row 属性。然后,我将该行转换为强类型数据表行(由数据集设计器生成)。现在,我将值分配给一些数据表行列。我在 BindingListCollectionView 上调用 CommitNew。最后,我在 CollectionViewSource 上调用 MoveCurrentToFirst。
问题:
使用监视表达式,我可以看到数据位于 CollectionView 和 BindingListCollectionView 的 SourceCollection 中。 任何人都可以解释为什么绑定控件不显示数据,除非我将更改保存到数据库?
代码(生成的 XAML 未显示):
Private WithEvents _cvsScanData As System.Windows.Data.CollectionViewSource
Private WithEvents _blcvScanData As System.Windows.Data.BindingListCollectionView
_cvsScanData = CType(Me.FindResource("Dt_tblScanDataViewSource"), System.Windows.Data.CollectionViewSource)
_blcvScanData = CType(_cvsScanData.View, BindingListCollectionView)
Dim newRow As LabDataSet.dt_tblScanDataRow = CType(CType(_blcvScanData.AddNew, System.Data.DataRowView).Row, LabDataSet.dt_tblScanDataRow)
newRow.SampleID = "testSampleID"
newRow.MachineID = "testMachineID"
_blcvScanData.CommitNew()
_cvsScanData.View.MoveCurrentToFirst()
I'm using wpf drag and drop databinding to datasets. I generate a new datatable row using the AddNew method of the BindingListCollectionView. I set values on the new row and call CommitNew on the BindingListCollectionView. I expect to see my assigned values in the bound controls but they are all blank. If I save the changes to the database the controls are then updated, but I want the user to see my assigned values before calling UpdateAll on the TableAdapterManager.
Background:
I've created a strongly typed dataset in a project separate from my wpf application. My wpf app references the dataset application. I added an object datasource in the wpf app pointing to the typed dataset. I dragged fields/controls from the datasources window to the wpf designer window. The generated xaml includes a Window.Resources section with a CollectionViewSource accurately bound to my dataset and datatable. This CollectionViewSource is the DataContext for the controls that I dragged to the design surface. All of the controls use TwoWay databinding.
When the window loads I grab a reference to the xaml's CollectionViewSource (using FindResource). I then grab a reference to the view property of the CollectionViewSource and cast it to a BindingListCollectionView. Now I use the AddNew method of the BindingListCollectionView to generate a new row (which AddNew returns as an object). I cast the object to a DataRowView and access it's Row property. I then cast the row to a strongly typed datatable row (generated by the DataSet designer). Now I assign values to some of the datatable row columns. I call CommitNew on the BindingListCollectionView. Finally I call MoveCurrentToFirst on the CollectionViewSource.
Problem:
Using a watch expression I can see the data is in the SourceCollection of both the CollectionView and the BindingListCollectionView. Can anyone explain why the bound controls do not show the data unless I save the changes to the database?
Code (generated XAML not shown):
Private WithEvents _cvsScanData As System.Windows.Data.CollectionViewSource
Private WithEvents _blcvScanData As System.Windows.Data.BindingListCollectionView
_cvsScanData = CType(Me.FindResource("Dt_tblScanDataViewSource"), System.Windows.Data.CollectionViewSource)
_blcvScanData = CType(_cvsScanData.View, BindingListCollectionView)
Dim newRow As LabDataSet.dt_tblScanDataRow = CType(CType(_blcvScanData.AddNew, System.Data.DataRowView).Row, LabDataSet.dt_tblScanDataRow)
newRow.SampleID = "testSampleID"
newRow.MachineID = "testMachineID"
_blcvScanData.CommitNew()
_cvsScanData.View.MoveCurrentToFirst()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单的修复方法是在调用 CommitNew 后调用 BindingListCollectionView 的 Refresh 方法。
我通过智能感知偶然发现了我自己问题的答案。如果有人能解释为什么需要刷新,我将不胜感激。我希望 INotifyPropertyChange 接口能够更新绑定控件,从而无需调用刷新。
The simple fix is to call the Refresh method of the BindingListCollectionView after calling CommitNew.
I stumbled across this answer to my own question via intellisense. If anyone can explain why refresh is necessary I'd appreciate it. I expected the INotifyPropertyChange interface to update the bound controls obviating the need to call refresh.