I don't know how to properly return value from ComboboxColumn cell after selection. My ComboboxColum looks like this:
<DataGridComboBoxColumn CanUserSort="False"
x:Name="cbcList"
DisplayMemberPath="{Binding Person, Converter={StaticResource converter}}"
Header="Person"/>
ItemsSource
is set in the code behind as list of Person
objects. Now, how I can get list of Person
objects made from all selections in this column? I tried to make new list of Person
obejcts and bind it using SelectedItemBinding
, but it's either wrong way to do this or I made some mistakes.
The SelectedItem does not have a property setter which means you cannot assign a binding to it. We can, however, track multiple selections via the SelectionChanged event.
Assumption: You have an ObservableCollection in the ViewModel called People.
Create a Collection property called SelectedPeople in the ViewModel
Implement the SelectionChanged event in the XAML.
The SelectionChanged handler in the code behind will look something like this.
I have removed exception management for brevity.
Edit - The above is about handling multiple selections from a DataGrid. I have renamed the PersonModel above to Person. It is the model that exposes data to the view. The ViewModel is the class that manages the Model(s) and functionality used to manipulate the Model(s). For further information, google MVVM pattern.
With regards to your original question, handling the binding between a DataGridComboBoxColumn and the DataGrid, I have created an example. The code that follows can be copied into a new project.
The Person model is
And the ViewModel is
The Code behind the View simply creates and exposes the ViewModel.
Leaving all the fun in the Xaml.
The this namespace will need to be changes to the namespace of your project.
I hope this helps.