Click here to Skip to main content
15,071,262 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:

C#
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:

C#
<DataGrid
    AutoGenerateColumns="False"
    ItemsSource="{Binding MyObservableCollection}">

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

</DataGrid>


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..
Posted
Updated 31-Aug-21 0:43am
v2

1 solution

Set the properties on the new instance of your class to the appropriate default values:
C#
MyObservableCollection.Add(new MyClass
{
    PropertyName1 = "Default value for property 1",
    ...
});
   
Comments
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.
Richard Deeming 31-Aug-21 8:49am
   
Flidrip 31-Aug-21 9:01am
   
Interesting! What the accepted answer on that SO post did was give the ComboBoxColumn have a displayed value, but that value was the NAME of the property, not the data therein. And also, it changed the two original rows to be the same. What's more, it also made it so that now my ComboBoxColumn have 3 items, instead of 2 to choose from, and each time I add a row, it adds another item to the collection.. lol. The last one is a separate issue I am sure, as that was there before (but if you have any idea as to why, I'd sure welcome it)

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