locked
how do I bind a name/id collection to a combobox control ? RRS feed

  • Question

  • How do I bind a two element collection passed through a wcf service to a combobox?

    As you can see by the service below, I have a value WatchID whose value I want to correspond to the Name which I would like displayed.

    What I have in the proxy below displays my string value like this; http://tinyurl.com/dd65dr  "Greg's Watch List"

    Thank you for your help!

    void proxy_GetWatchListNameandIDForDropDownCompleted(object sender, Fusion.ServiceReference1.GetWatchListNameandIDForDropDownCompletedEventArgs e)  {

           ComboBoxCustomWatchLists.ItemsSource = e.Result.ElementAt(0).WatchListName.ToString();    }

    **********  Service.svc.cs  *********************

    [OperationContract]
    public List GetWatchListNameandIDForDropDown(string UserID)
    {
      DataClassesGetWatchListNameandIDforDropDownDataContext db = new DataClassesGetWatchListNameandIDforDropDownDataContext();
      var DropDownList = db.prcGetWatchListNameandIDforDropDown(UserID).ToList();
      var tempDropDownList = new List();
      foreach (prcGetWatchListNameandIDforDropDownResult list in DropDownList)
      {
         WatchListNameandIDforDropDown objDropDownList = new WatchListNameandIDforDropDown();
         objDropDownList.WatchListName = list.Name;
         objDropDownList.WatchID = list.WatchID.ToString();
         tempDropDownList.Add(objDropDownList);
      }
      return tempDropDownList;
    }

     
    Thursday, April 16, 2009 6:35 PM

Answers

  •  This code seems alright to me. But can you post the xaml snippet for the combo box and where you set the itemsource?

    The XAML needs to have the DisplayMemberPath field set as WatchListName and in the xaml.cs you need to set the itemsource to this list.

    Thursday, April 16, 2009 7:38 PM

All replies

  •  This code seems alright to me. But can you post the xaml snippet for the combo box and where you set the itemsource?

    The XAML needs to have the DisplayMemberPath field set as WatchListName and in the xaml.cs you need to set the itemsource to this list.

    Thursday, April 16, 2009 7:38 PM
  • Thank you. That did it!  How can I make the first item returned the selected item? For any others using WCF, getting the value is shown below.

                <ComboBox
                     Name="ComboBoxCustomWatchLists"

                    DisplayMemberPath ="WatchListName"

    
                     ItemsSource="{Binding}" >
                </ComboBox>
    private void ComboBoxCustomWatchLists_SelectionChanged(object sender, SelectionChangedEventArgs e)   {

         object selectedItem = this.ComboBoxCustomWatchLists.SelectedItem;

         string ID = (selectedItem == null) ? string.Empty : ((Fusion.ServiceReference1.WatchListNameandIDforDropDown)selectedItem).WatchID;

         string Name = (selectedItem == null) ? string.Empty : ((Fusion.ServiceReference1.WatchListNameandIDforDropDown)selectedItem).WatchListName;

    }

    Thursday, April 16, 2009 8:04 PM
  • After you populate the combo, just set the selected index to 0 and that should select the first item.

    Thursday, April 16, 2009 11:24 PM