I have a Combobox which defaults to the first item in the list from ItemsSource using
<ComboBox x:Name="combo"
SelectedIndex="0"
ItemsSource="{Binding comboBoxSelections, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"
DisplayMemberPath="Key"
IsEditable="True" >
However, below doesn’t bind properly to the a DataTable.
When I add binding to SelectedValue it now binds correctly but no longer defaults to the first item anymore;
SelectedIndex="0" isn’t working now
<ComboBox x:Name="combo"
SelectedValue="{Binding Selection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedIndex="0"
ItemsSource="{Binding comboBoxSelections, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"
DisplayMemberPath="Key"
IsEditable="True" >
Is it possible to initialise the value in XAML only?
I don’t think I can set it from the constructor as combo is not a single Combobox but part of a DataGridComboBoxColumn
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Department Id" x:Name="comboboxColumn1"
SelectedValueBinding="{Binding Department Id}" />
<DataGridTemplateColumn x:Name="DataGridTempCol" Header="Selection">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox
x:Name="combo"
SelectedValue="{Binding Selection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding comboBoxSelections, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"
DisplayMemberPath="Key"
IsEditable="True" >
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
Set the SelectedValue to the first row in the ctor.