locked
Bind ComboBox via ID RRS feed

  • Question

  • I have a Category class with a Name field and ID field. Then I have a User class where one of the fields is an instance of Category. Finally I have a seperate class that has a list of all the Categories for the ComboBox to bind to (because I dont want to keep the list on every user object.

    <ComboBox SelectedValue="{Binding Path=CategoryID, Mode=TwoWay}" SelectedValuePath="ID" ItemsSource="{Binding Source={StaticResource categories},Mode=TwoWay, Path=AllEventCategories}" DisplayMemberPath="Name"   />
                        

    So that's what I have on my form and it binds to the list of Categories fine. But I cannot get it to automatically select the right one based on the DataContext's CategoryID field which is the byte ID of the Category.

    Help would be appreciated.

    Sunday, January 1, 2012 11:51 AM

Answers

  • Try moving the ItemsSource attribute to before the SelectedValue attribute or bind SelectedItem to the instance of category.

    Sunday, January 1, 2012 12:35 PM

All replies

  • Try moving the ItemsSource attribute to before the SelectedValue attribute or bind SelectedItem to the instance of category.

    Sunday, January 1, 2012 12:35 PM
  • Awesome, the former worked!

    Sunday, January 1, 2012 3:28 PM
  • I think the SelectedValue and the like may have been a holdover from early versions of SL.  Not sure, but I find it more consistent with other controls to have:

    ItemsSource

    SelectedItem

    and when necessary

    DisplayMemberPath

    Monday, January 2, 2012 11:00 AM