I want to show a ComboBox
with OPTGROUP
style header gruopings in Silverlight. Every website I find (including questions on SO) that sovle this link to an outdated link and, handily, show no code snippets for me to work from.
E.g.:
So how do I do this?
Group header is not supported in ComboBox. Alternate way for to implement it with DataGrid or use TreeView to show grouped data.
However, you can try something like this,
Silverlight Custom ComboBox
Here is one generalized approach. It's not bad, and should be flexible enough -- although it has the weakness of requiring an extended collection class (cannot make use of CollectionViewSource).
Step 1 ComboBoxGroupHeader object
This plays the role of "dummy entry" mentioned by @NiteshChordiya.
Step 2 Extended ComboBox
This overrides
PrepareContainerForItemOverride
, in order to tinker with the dummy items' containers. It also provides an (optional) "HeaderTemplate".Step 3 Extended collection class
This does the grouping, and adds the dummy "ComboBoxGroupHeader" entries. This implementation is essentially read-only (groupings would break if you tried to add new items), but it would be straight-forward to support operations like "Add", "Insert", etc.
Usage
It works like a normal
ComboBox
, with the optionalHeaderTemplate
:For the grouped to take effect, the source must use the "GroupedCollection" above, so as to include the dummy header items. Example:
See my similar question: How to show group header for items in Silverlight combobox? I put dummy entries in collection source to indicate the group header and then modified DataTemplate to show the group headers in different way and normal entries in separate way.