0

I wrote a WPF application in which my ComboBox.Items.IndexOf(ItemName); giving -1 even though Item is present in the ComboBox items list. Can anybody please help me to get the solution.

Thanks in advance

  • 3
    Can you please post a short, simple, and concise code example? http://sscce.org/ –  Sep 19 '11 at 13:17
  • 3
    IndexOf will not work on ItemName but on Item object itself. Can you show us databinging code or at least collection you have bind your combobox to? – Ales Ruzicka Sep 19 '11 at 13:18
  • @Turtleneck, in WPF items controls (such as `ComboBox`), the `Items.IndexOf()` isnt a search by name. Also `ComboBox.Items` are not **always** `ComboboxItems` they can be the individual items of a collection bound to the combobox. To search a comboboxItem you will have to refer the concept of `ItemsContainer`... see this... http://stackoverflow.com/questions/165424/how-does-itemcontainergenerator-containerfromitem-work-with-a-grouped-list – WPF-it Sep 19 '11 at 13:19

2 Answers2

2

.IndexOf is looking for the object in the items collection rather than looking for it by name (which is what you are indicating that you want to do). You should use ComboBox.FindStringExact("ItemName") to get what you require.

starskythehutch
  • 3,328
  • 1
  • 25
  • 35
0

I do believe that this is how you set the list to blank, hence the " - " before the 1, indicating that it's below the 0 threshold which items begin to be indexed.

Nicky40
  • 35
  • 3