Once upon a time there were four friends – two Text Boxes and two Combo Boxes.They all lived happily on a WPF Window bound together in blissful MVVM harmony.There wasn’t much documentation regarding that which I found that was helpful, so I’ll do another post about that later.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.On cancel, I wanted to disregard the Combo Box change and revert back to the previously selected entity.) So based on business rules, we’ve ignored a change to the view model.The Text Boxes always remain synchronized – reverting back to the previous value if the change is ignored.For those of you who want to play around with the code, you can find it here. The code sample now includes his behaviour code in addition to the subclassing option. He is the creator of psake, a Power Shell-based build automation tool, intended to save developers from XML Hell.James is the Ruby Track Chair for Dev Teach, one of Canada’s largest independent developer conferences.
Fortunately in many cases you can work around these limitations by simply deriving your own custom control from the one supplied with WPF to fix these problems. Perry in this comment, WPF behaviours can also be used to inject code without subclassing Combo Box. The Combo Box (regulard, subclassed, or behavioured) becomes unsynchronized from the view model. He blogs on Code as well as his own blog, is a technical contributor for Pluralsight, writes articles for MSDN Magazine and Co De Magazine, and is a frequent speaker at conferences and user groups.Now, all of that aside, let’s go over the more common properties used in Data Binding to a Selector control.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.Examining the current state of the view model’s Combo Box Selected Item, its value is “Four” and not “Two”.I selected “Two” in the first Combo Box, which was the ignored change.