When adding items to a Combobox dynamically you might notice that setting an items IsSelected = true directly aftewards fails to work.
For example, this fails to select the first item in the ComboBox:
ComboBoxItem cbi;
foreach (string category in Categories)
{
cbi = new ComboBoxItem();
cbi.Content = category;
CategoryCB.Items.Add(cbi);
}
if (CategoryCB.Items.Count > 0)
{
cbi = (ComboBoxItem)CategoryCB.Items[0];
cbi.IsSelected = true;
}
The way to do this is via the SelectedIndex property off your ComboBox control:
if (CategoryCB.Items.Count > 0)
{
CategoryCB.SelectedIndex = 0;
}
Thanks,
–Mike







