Domanda

I have a combobox which I'm setting it's itemsSource after getting it from a database, for some reason the combobox doesn't show the first item.

My comboBox:

 <ComboBox Text="Add To Message:" ItemsSource="{Binding Messages}" SelectedValue="{Binding SelectedMessage}" Style="{StaticResource comboStyle}"/>

My style:

<Style TargetType="ComboBox" x:Key="comboStyle">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=SelectedItemId}" Value="-1">
                    <Setter Property="SelectedIndex" Value="1"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
È stato utile?

Soluzione

Instead of setting SelectedIndex you should set SelectedMessage:

// get data from db...

SelectedMessage = Messages.FirstOrDefault();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top