I'm trying to get a DataGridComboBoxColumn working with my ViewModel. Everything appears to work correctly but when I change the value of the combo box, the entity isn't changed.
The datacontext of the window has these properties:
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
I feel the problem lies with how I populate CurrentEnergySource in the ViewModel that is the 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
When I create the backing collection for the combobox, if the model is the selected one, I set that viewmodel to be the CurrentEnergySource but after that point it is disconnected(and that's the problem)
What should I reference in CurrentEnergySource so that it updates the model when the combo box changes?
of course, the problem is in your binding, the DataGridComboBoxColumn automatically take one item from CurrentEnergySource, and the CurrentEnergySource don't have AllEnergySources on it, Whey you don't semply Use this,
One thing that seems to be wrong is you should probably be using SelectedValueBinding not SelectedItemBinding.
Here's a sample that works ok for me:
Have you tried a RelativeSource on the binding? usually I find when I'm binding to a column in a template etc the binding looks inside the binding of the control, not that of the datacontext of the view.
Try either:
or
and then adding
x:Name="NameOfTheView"
to the View's attributes (below the xmlns place inside the > bracket)My answer is you need to change the foreign key manually (I now change it in the setter of CurrentEnergySource, which is the SelectedItemBinding bound property)
On load, populate the private backing store
_CurrentEnergySource
instead of the property to avoid all objects starting out with modified state