Wpf combobox selecteditem not updating
A few weeks ago, a friend convinved me to start looking into WPF, XAML and the MVVM pattern. NET Web Forms & MVC2 applications and core system design/development, I haven’t had any exposure to these new(er) technologies, and I thought it might be fun to see what all of the fuss is about. I am using terms in this post in a way which assumes that you are using the MVVM pattern.Note that the information in this post may also be applied to other controls implementing Items Control. Along the way, I came across a situation where I needed to do some different kinds of binding using Combo Boxes.In this post I’ll just cover what I learned about basic data binding, so here is what I needed to do, without all of the Data Template/Grid View bloat Note that in my case, the View Model which the Combo Box needed to get its items from was the Data Context of the page and the Data Context of the Combo Box was the View Model which I needed to bind values to.Now, all of that aside, let’s go over the more common properties used in Data Binding to a Selector control.You can read more about the different binding modes here. Background in the payment industry, particularly in card switching and related system architecture. Lover of high-performance distributed architecture. To set this up, let's define Category objects and Product objects like this: Category--ID--Category Name--Products (An IList)Product--ID--Product Name--Category Parent (A Category Object)In the detail form for the Product, you want to have a Combo Box that has as its Items Source the list of Categories.When a Product is selected, the Combo Box should change the selection to align with the Category Parent property of the Product.
Creating a Combo Box that displays the list is quite simple.Now that we’ve got a decent understanding of the common binding properties of a Selector control (I hope! Given the following classes (only class signatures; the full sample project including source code is attached to this post): Note the use of “Mode=One Way” in my Items Source bindings.This indicates to the binding that we’re only going to be updating the binding target (the Combo Box’s Items collection in this case).Let's tackle the second issue (changing the Category Parent value of the selected Product) first.The Selected Item property, when bound to the correct property of a Product, will allow the user to make updates.