Tech Off Thread

3 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Combobox.SelectedItem assignment not working when adding items statically

Back to Forum: Tech Off
  • User profile image
    xtr33me

    Hi everyone...hopefulyl find an answer here while I continue my search.  I am doing this in WPF .Net 4 and I simply want to set SelectedItem to a string value and have it select that item from the combobox. Now I am adding my items to the combobox statically during design time using Item collections property and when I do this, SelectedItem doesn't work and always is null. However I just found out that when I add my combobox items during runtime using combobox1.Items.Add, selectedItem works just fine. Now I would like to know why this is. I am guessing that it is some binding that automatically happens when adding them dynamically, but I would love to know how to get this working statically. Other werid thing is that SelectedIndex works just fine either way, it is only selectedItem and selectedValue that do not.  Thanks!

  • User profile image
    spivonious

    If you add the items in the XAML, WPF makes ComboBoxItem objects with the given string as its value. If you add the strings at runtime, it's adding strings. So when you set the SelectedItem to a string, WPF can't match it to a ComboBoxItem. Since you're adding things at design time, just use SelectedIndex.

  • User profile image
    xtr33me

    spivonious said:

    If you add the items in the XAML, WPF makes ComboBoxItem objects with the given string as its value. If you add the strings at runtime, it's adding strings. So when you set the SelectedItem to a string, WPF can't match it to a ComboBoxItem. Since you're adding things at design time, just use SelectedIndex.

    Thanks spivonious for the help!

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.