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:
<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..