Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# WPF Combobox select first item

Goodday,

I want my combobox to select the first item in it. I am using C# and WPF. I read the data from a DataSet. To fill the combobox:

DataTable sitesTable = clGast.SelectAll().Tables[0]; cbGastid.ItemsSource = sitesTable.DefaultView; 

Combo box XAML code:

<ComboBox     Name="cbGastid"     ItemsSource="{Binding}"     DisplayMemberPath="Description"     SelectedItem="{Binding Path=id}"    IsSynchronizedWithCurrentItem="True" /> 

If I try:

cbGastid.SelectedIndex = 0;  

It doesn't work.

like image 374
Roboneter Avatar asked Dec 09 '13 20:12

Roboneter


1 Answers

Update your XAML with this:

<ComboBox          Name="cbGastid"          ItemsSource="{Binding}"          DisplayMemberPath="Description"          SelectedItem="{Binding Path=id}"         IsSynchronizedWithCurrentItem="True"         SelectedIndex="0" />  // Add me! 
like image 168
Brian Avatar answered Sep 20 '22 06:09

Brian