-1

I have a ComboBox which is populated on Page Load event. Just after populating the ComboBox I call another method which returns a value that I want to make the default value of the ComboBox when Page loads. How can I change the selectedIndex to a value that is returned when I call another method?

XAML for ComboBox

<ComboBox Name="cboProductType" DisplayMemberPath="ProductTypeName" SelectedValuePath="ProductTypeID" SelectedIndex="0"/> 

Page Load event:

void OnProductDetailLoad(object sender, RoutedEventArgs e)
{
   GetServiceReference.Service1Client service = new GetServiceReference.Service1Client();
   service.GetProductDetailsCompleted += service_GetProductDetailsCompleted;
   service.GetProductTypeCompleted += service_GetProductTypeCompleted;
   service.GetProductTypeAsync();
   service.GetProductDetailsAsync(ProductId);
}

Populating ComboBox when Page Loads:

void service_GetProductTypeCompleted(object sender, GetProductTypeCompletedEventArgs e)
{
    cboProductType.ItemsSource = e.Result;
}

Calling the other method which returns a particular ProductTypeName. I tried to get the index of that particular ProductTypeName but returns -1 always.

void service_GetProductDetailsCompleted(object sender, GetServiceReference.GetProductDetailsCompletedEventArgs e)
{
   if (e.Result.Count != 0)
   {
      p.ProductID = e.Result[0].ProductID;
     int index = cboProductType.Items.IndexOf(e.Result[0].ProductTypeName);
    cboProductType.SelectedIndex = index;
}

Also is this a right approach for setting the SelectedIndex property? Say I have following items loaded in ComboBox in following Index order:

Index    DisplayMemberName(ProductTypeName)

   0           Color
   1           Size
   2           Variant
   3           N-size

and e.Result[0].ProductTypeName contains Variant so I now want SelectedIndex = 2 of ComboBox

I hope my question is clear

Huma Ali
  • 1,759
  • 7
  • 40
  • 66
  • 2
    `ComboBox.Items.IndexOf()` will return `-1` only when the particular item is not in the collection. so debug your program and check the value of `e.Result[0].ProductTypeName – sujith karivelil Jan 25 '16 at 09:21
  • I did. The value is present in my `combobox`. Is there any other approach I can use to set the Index? – Huma Ali Jan 25 '16 at 09:24
  • @un-lucky I have a simple requirement. Can you tell me how I can achieve it? The `SelectedIndex` is at 0 right now. I want to change the `SelectedIndex` – Huma Ali Jan 25 '16 at 09:40
  • 1
    When you are binding to data `..Itemsource = ...`, SelectedIndex is not valid. I believe you need the `SelectedValue` and you need to set it to the `ProductTypeID` you want selected. It might be just `Value` – Steve Jan 26 '16 at 20:21
  • @Steve can you answer my post instead of the comment so I can mark it? You made my day! I was getting crazy after this! – Huma Ali Jan 27 '16 at 11:52

1 Answers1

2

The SelectedIndex is only valid when you manually fill a combobox. If you are using the ItemsSource to fill the items in a combobox, you must define a SelectedValuePath which will then allows you to use the SelectedItem property to SET/GET the selected item. When using binding to a DataContext to bind the controls, the SelectedValuePath should be the property that links the Parent/Child together.

Steve
  • 5,585
  • 2
  • 18
  • 32