How to select an item in my WPF ComboBox based on data binding?

I have a DataContext set for the Book object. The book has properties: Title, Category.

I have a CollectionViewSource "categoryList" that contains a list of categories.

Question How to choose a category of books in this field?

<TextBox Text="{Binding Path=Title}"/> <ComboBox SelectedValuePath="Id" SelectedValue="{Binding Path=Category.Id}" SelectedItem="{Binding Path=Category}" ItemsSource="{Binding Source = {StaticResource categoryList}}" DisplayMemberPath="Name" /> 

The code above displays the title of the book correctly, and then displays a list of category names in the combo box. But he does not choose the category of books. Instead, it simply selects the first item in the list.

+7
data-binding wpf xaml
source share
2 answers

You get too attached; you need to set only SelectedValue and SelectedValuePath , or SelectedItem . In this case, it looks like you are actually trying to bind to a specific object. If you are trying to set the ComboBox Category property to your Book , and the current Book object has a link to the Category instance that is in the categoryList , then you should use SelectedItem and remove the bindings for SelectedValue and SelectedValuePath .

Edit

To talk a little about how this is done, SelectedValue intended to be used when you have a common piece of information to associate a related item with a property in the list source. For example, suppose I have a Book class with the CategoryID property.

 public class Book { public string CategoryID { get; set; } public string Title { get; set; } } public class CategoryID { public string ID { get; set; } public string Name { get; set; } } 

In this case, you will do:

 <ComboBox SelectedValue = "{Binding CategoryID}" SelectedValuePath = "ID" DisplayMemberPath = "Name" /> 

SelectedItem , on the other hand, means that the linked instance has an actual reference (or rather, an object that is equivalent) to an element in the linked list. So, suppose the Book class looks something like this:

 public class Book { public Category Category { get; set; } public string Title { get; set; } } 

In this case, you will do:

 <ComboBox SelectedItem = "{Binding Category}" DisplayMemberPath = "Name" /> 

But it’s very important to note that this will not work if the Book class does not refer to the same Category instance as in the list . If the links are different (even if the values ​​in the classes are equal), then this will not work, because the ComboBox will not be able to find the Category that is referenced in the Book in the list.

The real problem with the way you linked it on top (binding to Category.ID ) is that you are mixing schemes. You have a link, but instead you are trying to bind it to a key. All this will try to set the value in your link, will not try to change the link to your class .

+7
source share

Describe in the code what Adam says:

 <ComboBox SelectedValuePath="Id" SelectedItem="{Binding Path=Category}" ItemsSource="{Binding Source={StaticResource categoryList}}" DisplayMemberPath="Name" /> 

probably more appropriate. Generally, it is best to use a read-only SelectedValue and use the SelectedItem to select which item you want to select. When you bind SelectedItem to a Category book, it automatically sets the SelectedValue property for you.

If this still does not work, you can check if the binding to Category works correctly. In particular, adding a DebugConverter works well to ensure that the value you expect is related. You can see that using DebugConverter is the answer to this question.

-Doug

+2
source share

All Articles