【发布时间】:2012-02-16 21:54:12
【问题描述】:
我正在尝试让 DataGridComboBoxColumn 与我的 ViewModel 一起使用。一切似乎都正常工作,但是当我更改组合框的值时,实体没有改变。
窗口的数据上下文具有以下属性:
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
我觉得问题在于我如何在 ViewModel 中填充 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 中引用什么,以便在组合框更改时更新模型?
【问题讨论】:
-
你是否绑定了 TwoWay 并且 EnergySourceViewModel 是否实现了 INotifyPropertyChanged?
-
是的,是的,但问题是我绑定到 (CurrentEnergySource) 的属性有自己的支持字段。属性应该获取/设置什么而不是支持字段?
-
显示你是否绑定了 DataGridComboBoxColumn
-
可能是个愚蠢的建议,但您是否尝试过将
UpdateSourceTrigger=PropertyChanged放入绑定中?
标签: wpf vb.net entity-framework mvvm