Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've got my DataContext set to a Book object. Book has properties: Title, Category.

I've got a CollectionViewSource "categoryList" that holds a list of Categories.

Question: How do I select the book's Category in this combobox?

<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 Book's title correctly and then it displays the list of category names in the combobox. But it does not select the Book's Category. It instead just selects the first item in the list.

like image 974
Abby Fichtner Avatar asked Dec 03 '09 04:12

Abby Fichtner


People also ask

What happens when we select a value from a ComboBox in WPF?

Selected and Current Item Text property of ComboBox represents the text of the current selected item in a ComboBox. SelectedItem represents the first item in the currently selected items in a ComboBox. SelectedValue represents the value of the currently selected item in a ComboBox.

How does data binding work in WPF?

Data binding is a mechanism in WPF applications that provides a simple and easy way for Windows Runtime apps to display and interact with data. In this mechanism, the management of data is entirely separated from the way data. Data binding allows the flow of data between UI elements and data object on user interface.

How do I bind a dropdown in WPF?

This article shows how to bind data dynamically from the database and get the ComboBox selected Text and Value. In ComboBox Element set the attribute ItemSource="{Binding}". Here DisplayMemberPath helps to display Text in the ComboBox.

How do I bind a ComboBox in WPF MVVM?

Select Item and display member property. Here, in item source, we give the collection name and in selected item, we give single person property. We have to define the display member here and you have to give whatever we have to display in combo box dropdown list. Now, run the Application and show the result.


2 Answers

You're binding too much; you only need to set SelectedValue and SelectedValuePath, or SelectedItem. In this case, it looks like you're actually trying to bind to a specific object. If you're trying to have the ComboBox set the Category property on your Book and the current Book object actually has a reference to a Category instance that's in categoryList, then you should use the SelectedItem binding and remove the bindings for SelectedValue and SelectedValuePath.

Edit

To expand a little on how this is done, SelectedValue is designed to be used when you have a common piece of information to link your bound item with a property on the list source. For instance, let's say I have a Book class with a 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 would do:

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

SelectedItem, on the other hand, is for when the bound instance has an actual reference to (or, more precisely, an object that is equivalent to) an item in the bound list. So, let's assume the Book class actually looks like this:

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

In this case, you would do:

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

But it's very important to note that this will not work unless the Book class has a reference to the same instance of Category as you have in the list. If the references are different (even if the values on the classes are equal) then this won't work, as the ComboBox won't be able to find the Category referenced in the Book in the list.

The real problem with the way you were binding it up top (by binding to Category.ID) is that you're mixing the schemes. You have a reference, but you're trying to bind on the key instead. All this will do is try to set the value on your reference, it will not try to change the reference on your class.

like image 134
Adam Robinson Avatar answered Oct 14 '22 16:10

Adam Robinson


To describe in code what Adam's talking about:

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

is probably more appropriate. Generally it's better to treat SelectedValue as read-only, and use SelectedItem to choose which item you want to select. When you bind the SelectedItem to the book's Category, it automatically sets the SelectedValue property for you anyway.

If that still doesn't work, you might want to check that your binding to Category is working properly. In particular, adding a DebugConverter works well to ensure the value you expect is being bound. You can see the use of DebugConverter is the answer to this question.

-Doug

like image 33
Doug Avatar answered Oct 14 '22 15:10

Doug