0

I need my ComboBox's first item to be selected when loaded.

XAML:

 <ComboBox x:Name="CreateLstBoxFormat" HorizontalAlignment="Left" Margin="27,334,0,0" IsSynchronizedWithCurrentItem="True" VerticalAlignment="Top" Width="90"  SelectedValuePath="Content" SelectedIndex="0" SelectedItem="{Binding CreateFormatSelectedItem, Mode=TwoWay}">
    <ComboBox.ItemsSource>
        <x:Array xmlns:sys="clr-namespace:System;assembly=mscorlib" Type="{x:Type sys:String}">
            <sys:String>MXF</sys:String>
            <sys:String>Quicktime MOV</sys:String>
            <sys:String>DPX</sys:String>
            <sys:String>TIF</sys:String>
            <sys:String>TGA</sys:String>
            <sys:String>CIN</sys:String>
            <sys:String>EXR</sys:String>
        </x:Array>
    </ComboBox.ItemsSource>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <i:InvokeCommandAction Command="{Binding Create_FormatSelectCommand}" 
                                    CommandParameter="{Binding YourCommandParameter}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ComboBox>

I expected MXFto be initially selected in the combo box on opening of the window.

I'm using the SelectedItem property to fire an event when the selected item has changed.

SharpGobi
  • 144
  • 1
  • 13
Dan Sewell
  • 1,278
  • 4
  • 18
  • 45
  • what the initial value of CreateFormatSelectedItem ? furthermore , try removing this property and see if the first one is getting selected ( just to narrow down the possibilities) – eran otzap Jul 16 '15 at 10:17
  • secondly place IsSynchronizedWithCurrentItem on your ComobBox – eran otzap Jul 16 '15 at 10:18
  • 2
    @eranotzap This is totally an invalid edit, Post answer. Don't put your answer in the question. – Shaharyar Jul 16 '15 at 10:22
  • possible duplicate of [C# WPF Combobox select first item](http://stackoverflow.com/questions/20479976/c-sharp-wpf-combobox-select-first-item) – Shaharyar Jul 16 '15 at 10:22
  • Thirdly , when writing a question try to specify ONLY RELEVANT info. Explain your self in less words , avoid spelling mistakes , put an emphasis on grammar , PUT THE MOST RELEVANT INFO First (SelectedIndex , SelectedItem , add the CS property bound that item , show that the binding works be recheacking your DataContext and state that you've seen the getter of that property being called. – eran otzap Jul 16 '15 at 10:23
  • @Shaharyar i didn't answer any thing. he posted allot of irrelevant data in his post. – eran otzap Jul 16 '15 at 10:24
  • There was no irrelevant data in my opinion. Its the code im using to perform this function. – Dan Sewell Jul 16 '15 at 10:25
  • 2
    You should not edit the question in a way that changes its scope and context, you should just fix things like syntax, urls, readability etc – Shaharyar Jul 16 '15 at 10:27
  • yes cause i really care about your margin alignments and interaction triggers , expacily if they pushed aside the most relevant piece of data there the SelectedItem property – eran otzap Jul 16 '15 at 10:27
  • @Shaharyar i just illustrate how a good clean question should be written. minimal words , minimal xaml . let the user read it in one pass. – eran otzap Jul 16 '15 at 10:28
  • @eranotzap - As others have said, please don't edit the code in someone's question. You can clean up the wording and formatting, but you must respect the original intentions of the asker. For example, this was also an improper edit: http://stackoverflow.com/posts/30720591/revisions – Brad Larson Jul 16 '15 at 18:07
  • Did you resolved the issue? – Noam M Jul 20 '17 at 05:30

2 Answers2

1

Try using SelectedItem property:

<ComboBox Name="myComboBox" 
          ItemsSource="{Binding}" 
          DisplayMemberPath="Description" 
          SelectedItem="{Binding Path=id}"
          IsSynchronizedWithCurrentItem="True"
          SelectedIndex="0" />
Noam M
  • 3,156
  • 5
  • 26
  • 41
0

I think using

SelectedIndex="0(or your desired index)"

will do that for you, even if you are working with MVVM approach. if you don't select anything then it will always show it as blank initially.

DeshDeep Singh
  • 1,817
  • 2
  • 23
  • 43
  • I am already using that, but i have found the 'SelectedItem' property seem o be overriding it. SelectedIndex works if I remove SelectedItem – Dan Sewell Jul 16 '15 at 10:59
  • it should not happen in any case if you have really followed mvvm perfectly, because seleted index doesnt have any relation with selected item. – DeshDeep Singh Jul 16 '15 at 11:17