tags:

views:

454

answers:

1

Is there a way to set the initial value of a databound combo box in XAML?

Here is my combo box:

<ComboBox 
    IsSynchronizedWithCurrentItem="True" 
    ItemsSource="{Binding Source={StaticResource NHibernateTypes}}"
    SelectedValue="{Binding ElementName=IdentifierPoperty, Path=PropertyType}"
    HorizontalAlignment="Stretch" 
    VerticalAlignment="Top" 
    Grid.Row="1" 
    Grid.Column="1" 
    Margin="0,5,10,0"/>

The ItemsSource is an ObjectDataProvider that gets values from an enum. I would like to have the the first value in the enum displayed in the combo box by default. I tried setting SelectedIndex="0", but nothing happened.

Here's the really odd thing. I have a half dozen of these combo boxes in my window, all declared in the same manner as above. About half of them show the first value in their enum by default, but the others are blank. I have verified that all the bindings are working--each combo box shows the correct drop-down when I click it.

Is there a way to explicitly set a default index value for these combo boxes? Is there any reason some sombo boxes would show a value by default while others do not? Thanks for your help.

David Veeneman
Foresight Systems

+1  A: 

Found my answer--I was using the object data provider incorrectly. I had created an ODP for each enum I needed to bind to a combo box, with the result that several combo boxes were bound to the same ODP. That's what was causing the problem. I changed my ODPs to provide one ODP for each combo box (some ODPs read the same enum) and that solved the problem. My combo boxes now initialize themselves properly.

David Veeneman