Click here to Skip to main content
15,502,333 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My DataGrid is bound to an ObservableCollection in my ViewModel. I add new rows via a command that looks like this:

public void AddItem() {
    MyObservableCollection.Add(new MyClass());

How can I set default value for a DataGridComboBoxColumn when I create new rows, because as is - it turns up empty, which mess up my bindings.
If I could, for example, bind it to Item at index 1, or something, that'd be great.

The XAML for the DataGrid and the DataGridComboBoxColumn looks like this:

    ItemsSource="{Binding MyObservableCollection}">

         ItemsSource="{Binding Source={StaticResource MyObservableCollection}}"
         SelectedValueBinding="{Binding PropertyName1}"
         SelectedValuePath="PropertyName1" />


What I have tried:

I tried setting the TargetNullValue and FallBackValue, but it did not work. I also tried setting the DefaultValue when instantiating the new row, but it did not work. I would prefer a MVVM-solution, but am also fine with a code-behind hack..
Updated 31-Aug-21 1:43am

1 solution

Set the properties on the new instance of your class to the appropriate default values:
MyObservableCollection.Add(new MyClass
    PropertyName1 = "Default value for property 1",
Share this answer
Flidrip 31-Aug-21 6:53am    
Thank you @Richard Deeming! Works very nice for my TextBoxes. But how would I set the DefaultValue of a ComboBoxColumn, is it possible to reference a index, f.ex 1? Seeing as though it'd differ depending on whos profile the user is visiting at the time?
Richard Deeming 31-Aug-21 7:03am    
You can only set properties to a specific value. To set it by index, you'd need to access the collection that your combobox is bound to and retrieve the appropriate value for the property.
Flidrip 31-Aug-21 7:46am    
Richard, hm, I am a bit confused. I thought I was accessing the collection, as "MyCollectionName" is the ItemsSource of the ComboBoxColum. It's bound to a StaticResource, I don't know if that makes a difference.
Richard Deeming 31-Aug-21 8:35am    
The code in your question seems to be binding both the grid and the combobox within the grid to the same collection, which doesn't make much sense to me. Are you trying to edit some sort of relationship between the items in the grid?
Flidrip 31-Aug-21 8:44am    
The ComboBoxColumn did not manage to access the property in the collection from the DataGrids ItemsSource (but the other columns could), so the collection had to be set as a StaticResource. Then I could access it.
But I get what you're saying. The other columns in the DataGrid have no relationship to the ComboBoxColumn (i just do math with them, and print them to excel) so in reality they could be of different collections, but I didn't figure out how to give the DataGrid two itemssources.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900