In WPF it’s quite easy to bind a combo box to an enumerated type. Unfortunately in Silverlight, I found myself in for a world of pain.
In the end, I found the following solution (starting off from this blog)
First, we need to collect up all of the names in our enumerated type. We due this by using Reflection.
The static method above returns an observable collection of all of the individual public named values in an enumerated type, by walking over the fields, getting the name, and then parsing the name back into the enumerated type itself.
Next we need to define our editing template in our DataGrid.
The key here is the binding to a StaticResource. I initially tried to bind to a property of an element name, but because this is in a data template, the LogicalTree is yet available, and the element fails to bind.
Finally, there’s the matter of setting the static resource. Rather than create a new object so I could construct it in XAML, I decided it would be better to dynamically add the object to the StaticResources in the code behind. Here’s how I did it:
First, we define our enum.
Notice that the enum starts with 1. That’s something else I found out along the way on this project. MySQL database enums reserve 0 as an undefined value, but that will have to wait for another blog entry.
Next, we need to create our collection to be used as the ItemsSource as the first line of our objects constructor.