Automatic selection of the first Comboxbox item

I have a ComboBox that binds to a ViewModel property.

<ComboBox  ItemsSource="{Binding UserCollection}"   SelectedValuePath="Id" DisplayMemberPath="UserName"   SelectedValue="{Binding SelectedUser}"       />

      

I want to auto select the first item when it has items.

I am using IsSynchronizedWithCurrentItem="True"

and SelectedIndex="0"

but not selecting items.

+3


source to share


2 answers


Could you please add your binding mode as two way



<ComboBox  ItemsSource="{Binding UserCollection}"    DisplayMemberPath="UserName"   SelectedItem="{Binding Path=Id}" IsSynchronizedWithCurrentItem="True" SelectedIndex="0"/>

      

0


source


Bind SelectedItem

instead SelectedValue

:

<ComboBox ItemsSource="{Binding UserCollection}"  
          SelectedItem="{Binding SelectedUser}"     
          SelectedValuePath="Id"
          DisplayMemberPath="UserName"  
 />

      



Then set SelectedUser

to the first element of your list (if not already set):

SelectedUser = UserCollection[0];

      

0


source







All Articles