2013-06-29 27 views
0

我有一個ObservableCollection(OF T),其中 「T」 是EF的實體一個ListView數據源。 要添加/編輯項目,使用IEditableCollectionView。它完美的作品。問題是取消修改現有項目的操作。IEditableCollectionView&的EntityFramework

Private Sub edit_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles ListBoxMain.MouseDoubleClick 

    If ListBoxMain.SelectedItem Is Nothing Then 
     MessageBox.Show("No item is selected") 
     Return 
    End If 

    Dim editableCollectionView As IEditableCollectionView = TryCast(ListBoxMain.Items, IEditableCollectionView) 

    Dim win As New ChangeItemWindow() 
    editableCollectionView.EditItem(ListBoxMain.SelectedItem) 
    win.DataContext = ListBoxMain.SelectedItem 

    If CBool(win.ShowDialog()) Then 
     editableCollectionView.CommitEdit() 
    Else 
     editableCollectionView.CancelEdit() 
    End If 

End Sub 

的方法「editableCollectionView.CancelEdit()」,下面拋出異常:「的CancelEdit不支持當前編輯元件」。 我想看看是什麼讓我的財產「CanCancelEdit」,並且是「假的」。

謝謝。

+0

什麼是實現IEditableCollectionView類?它是你的嗎? – Crono

回答

0

你應該叫CanCancelEdit()來檢查,你可以的CancelEdit()。

爲了使這項工作,集合中的對象需要實現IEditableObject:有是如何使這種情況發生在http://msdn.microsoft.com/en-us/library/system.componentmodel.ieditableobject(v=vs.110).aspx

IEditableObject主要用於DataGridRows一個例子。

在這種特殊情況下,當你正在編輯一個項目,也許取消,不要亂與周圍的CancelEdit。創建一個ViewModel類,並在對話框中顯示。如果,且僅當用戶單擊確定,然後將值複製回來。

你CarViewModel需要執行INotifyPropertyChanged以獲得最佳效果,在對話框中結合...

dim car = ListBoxMain.SelectedItem as Car; 
dim carVM as new CarViewModel 
carVm.Make = car.Make 
carVM.Color=car.Color} 
dim win as new ChangeItemWindow() 
win.DataContext = carVM 
if win.ShowDialog() then 
    car.Make = carVM.Make 
    car.Color = carVM.Color 
end if