I have a ComboBox whose XAML looks as follows:
<StackPanel Grid.Row = "0" Style="{DynamicResource chartStackPanel}">
<Label Content="Port:" HorizontalAlignment="Left" Margin="8,0,0,0"/>
<ComboBox Width="75" Height="24" HorizontalAlignment="Right" Margin="8,0,0,0" SelectedValue="{Binding Port, Mode=OneWayToSource}">
<ComboBoxItem Content="C43"/>
<ComboBoxItem Content="C46" IsSelected="True"/>
<ComboBoxItem Content="C47"/>
<ComboBoxItem Content="C48"/>
</ComboBox>
</StackPanel>
The style referenced above is defined as follows:
When the ComboBox is first displayed, I’d like the ‘C46’ item displayed in the ComboBox. However, when this loads, the ComboBox is blank. What’s interesting is that the source property, in my VM, is getting set to ‘C46’. Can anyone tell what I’m doing wrong?
You mention that you have a source collection in your ViewModel. Therefore, why do you specify ComboBoxItems one by one in XAML? I think you should have in your ViewModel a Collection of Items property and also SelectedItem property. In the constructor you can set the SelectedItem. It can looks as follows:
And in your constructor after Items property is initialized:
Your XAML can looks as follows then: