Last week, I encountered a problem doing some fiddly UI behaviour in a Prism/MVVM app. I wanted something like this:
I wanted a ComboBox where you can choose one of the available options, or none of them. Null, deselected, whatever.
Unfortunately for me, WPF doesn't support binding null values in ComboBoxes, ListBoxes or ListViews. Now this is a pretty common problem, and a quick google reveals a few solutions available to this problem:
- Add an "IsNull" property to your models
- Wrap the ComboBox in an adapter that provides null support
- Create an attached property that clears the selected item when the user hits DEL
However, if like us, your domain model is all nicely decoupled through interfaces, the easiest way to get null values into a ComboBox is with the null object pattern. (And thankfully for me doesn't require any knowledge of WPF control internals to implement).
Here's what my null object looked like:
To use it, you simply pop a null object at the start of your list. And check whether it was selected on submit:
XAML binding is standard; no need to change anything because we handle it all in the view model. Which arguably is not the most DRY solution long-term, but sufficient until I learn more about WPF internals to do it elegantly with XAML.