我想获得一个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