我想獲得一個DataGridComboBoxColumn與我的ViewModel一起工作。一切似乎正常工作,但當我更改組合框的值時,該實體不會更改。如何使用MVVM將DataGridComboBoxColumn綁定到EntityFramework?
窗口的DataContext的具有以下屬性:
的ItemsSource
Public Property AllEnergySources() As ObservableCollection(Of EnergySourceViewModel)
SelectedItemBinding
Private _CurrentEnergySource As EnergySourceViewModel
Public Property CurrentEnergySource() As EnergySourceViewModel
Get
Return _CurrentEnergySource
End Get
Set(ByVal value As EnergySourceViewModel)
_CurrentEnergySource = value
OnPropertyChanged("CurrentEnergySource")
End Set
End Property
我覺得問題在於我怎麼填充CurrentEnergySource在視圖模型那就是DataContext:
Sub New(SelectedEntity as EquipmentEnergySource)
AllEnergySources = New ObservableCollection(Of EnergySourceViewModel)
//Select all EnergySources from the EntityFramework
Dim EnergyEntities = From esr in db.EnergySources Select esr
//Loop through to convert Entity POCO to Collection of ViewModels
For Each es In EnergyEntities
_AllEnergySources.Add(New EnergySourceViewModel(es))
//Optionally Set the newly created ViewModel to SelectedItemBinding object
If es.EnergySourceID = SelectedEntity.EnergySourceID Then
_CurrentEnergySource = _AllEnergySources.Last
End If
Next
End Sub
當我創建的後盾收集組合框,如果模型中選擇一個,我設置視圖模型是CurrentEnergySource,但在那之後它斷開(這就是問題所在)
我應該在CurrentEnergySource中引用什麼,以便在組合框更改時更新模型?
你是否雙向綁定和不EnergySourceViewModel執行INotifyPropertyChanged? – Paparazzi 2012-02-16 22:07:18
是的,是的,但問題是我綁定到的屬性(CurrentEnergySource)有其自己的支持領域。而不是支持領域該物業應該得到/設置什麼? – Michael 2012-02-16 22:26:46
顯示你綁定DataGridComboBoxColumn – Paparazzi 2012-02-16 23:21:46