Is this control really necessary? Is there a better way?
As @lawc points out, no, it is not necessary. It may, however, be preferable, depending on what level of flexibility you desire. A reusable style is easy enough to create, but doing it "correctly" is a bit more involved than simply setting a custom ItemTemplate
.
Using Styles
An ItemsControl
in WPF will wrap its items in appropriate containers. Each of the selector controls in core WPF overrides the logic which determines whether an item is capable of serving as its own container, as well as the factory code which produces new item containers. A ListBox
, for example, will wrap each of its items in a ListBoxItem
(unless the item itself is already a ListBoxItem
). The style applied to these containers can be set for the parent ItemsControl
via the ItemContainerStyle
property. This differs from the ItemTemplate
property, which allows you to control the appearance of the item within the container. More specifically, it overrides the content template applied to the ContentPresenter
within the container.
Since a RadioButton
does not derive from ListBoxItem
, simply setting the ItemTemplate
will produce a list of RadioButton
controls embedded within ListBoxItem
controls, which means they will still have the same selection chrome normally associated with ListBox
controls, and possibly some layout and focus oddities. This is probably not what you want.
Instead, override the ItemContainerStyle
and use it to assign a custom ListBoxItem
template which embeds a RadioButton
. You can probably get away with not setting the GroupName
property at all, which eliminates possible name collisions. Instead, just establish a two-way binding between the RadioButton.IsChecked
property and the templated parent's ListBoxItem.IsSelected
property.
In order to use this technique conveniently, one generally creates a Style
resource (available application-wide) which can be applied to the appropriate ListBox
instances, and which sets the ItemContainerStyle
. Alternatively, you can make the container style available as a global resource and set that on your ListBox
instances. Either way, you need to set a property.
Using a Custom Control
While WPF evangelists often recite the philosophy of preferring custom styles over custom controls, in practice this is not always convenient. You may find it more convenient to create a RadioButtonList
which extends the ListBox
control, and then give it a default style which automatically applies the custom style described above. This gets you out of manually assigning the list style or container style on every ListBox
instance, but it's not a huge win.
But maybe you want a bit more control over the appearance of the RadioButton
items. For instance, you may want to:
- Adjust the margin around the "bullet" of each
RadioButton
item;
- Adjust the vertical alignment of the bullets relative to the content;
- Support both horizontal and vertical orientations;
- Automatically disable the
RadioButton
content for items which are not selected.
Creating your own implementation, most likely derived from ListBox
, allows you to add these features easily, even after you are already using your radio list across your application. This could be done with the technique above too, though it may require an attached behavior or some attached properties, in which case you end up with a somewhat fragmented design.
Third-Party Solutions
Is there a professional control, open-source or otherwise, that will allow me to get the benefits of data-binding without the headaches?
This is not an uncommon use case, and I have no doubt there are some implementations floating around. Some may be in open source frameworks, and some may be extracted from open source applications. As for third-party implementations, I do know that Actipro ships a RadioButtonList
in their Shared WPF library, which is included with all of their WPF components. When last I checked, it was not available on its own. It does, however, support all of the additional features I listed above.