WPF: ComboBox with selecteditem set make not use of SelectedIndex=0 ?

Posted by msfanboy on Stack Overflow See other posts from Stack Overflow or by msfanboy
Published on 2010-06-02T19:52:40Z Indexed on 2010/06/02 19:54 UTC
Read the original article Hit count: 1106

Hello,

Why is the first element in my combobox popup menu not shown in the selected item area of

my combobox , when I use the SelectedItem binding? Without that it is showing up ?? Using

the same code selecteditem + selectedindex that is no problem!

<ComboBox
        ItemsSource="{Binding SchoolclassSubjectViewModels}"
        SelectedItem="{Binding SelectedSchoolclassSubjectViewModel}"   
        SelectedIndex="0"
        Height="23"
        HorizontalAlignment="Left"
        Margin="375,13,0,0"
        VerticalAlignment="Top"
        Width="151">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding SchoolclassName}" />
                    <TextBlock Text=" " />
                    <TextBlock Text="{Binding SubjectName}" />
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

Well as workaround I used:

SchoolclassSubjectViewModels.Add(schoolclassSubjectVM);
        SelectedSchoolclassSubjectViewModel = schoolclassSubjectVM;

and this:

SelectedItem="{Binding SelectedSchoolclassSubjectViewModel,Mode=TwoWay}"

but I would prefer the xaml only way as it should really work.

© Stack Overflow or respective owner

Related posts about wpf

Related posts about combobox