Search code examples
wpfcustom-controlsradiobuttonlist

Is a custom `RadioButtonList` based on `ListBox` necessary in WPF?


I inherited a project that uses a RadioButtonList which inherits from a ListBox. It was taken off the web (currently cannot find a link to), and contains RadioButtonList.cs (which contains six dependency properties) and RadioButtonList.xaml (which is just styles and control templates).

This control is used in over a hundred places. It causes problems because it is not a complete and professional control. Problems such as, focus issues, keyboard navigation, and so on. (See comments.)

After much research at different times over the last couple years, it seems that this control is really not necessary. All that is needed is to set the GroupName property on a group of radio-buttons. And, the only reason why a RadioButtonList control is used is to help with data-binding a list of options through the inherited ListBox.

1) Is this control really necessary? Is there a better way?

2) Is there a professional control, open-source or otherwise, that will allow me to get the benefits of data-binding without the headaches? (We use Infragistics and DevExpress, but I am not familiar with all the controls these suites offer.)

My Answers

1a) Is this control really necessary?

  • If you only need one list of radio buttons, then no this control is not necessary.
  • If your applicaton uses many lists of radio buttons, then yes this control is necessary.
  • If you use a list of radio buttons in different applications, then yes this control is probably necessary.

1b) Is there a better way?

  • I say that deriving from a ListBox, ItemsControl, or whatever then creating styles and templates is the only way to create this control; therefore, no there is no better way.

2) Is there a professional control...

  • Definitely, the ListBoxEdit with the RadioListBoxEditStyleSettings.

Comments Regarding Answers

All the answers indicate that creating a RadioButtonList control is not necessary. Yet, if you need more than a couple lists of radio buttons, by the time you create the styles and control templates and maybe data template, you will end up with a collection of code artifacts that can be called a radio-button-list-control. Therefore, in my opinion, a RadioButtonList is necessary.

Moreover, my understanding is a RadioButtonList was dropped in an early WPF CTP. Which I can understand, because of the limited need for such a control that can easily be created.

Comment Regarding Accepted Answer

2) Is there a professional control...

  • Definitely, the ListBoxEdit with the RadioListBoxEditStyleSettings.

Lastly Comment on Mike Strobel's Answer

The RadioButtonList that I have is the end-result of his answer. While I am good at creating custom-controls, I rather let third-party component makers, such as Infragistics and DevExpress, create and support a basic control like this one.


Solution

  • I can only tell you that DevExpress uses a ListBoxEdit with a RadioListBoxEditStyleSettings to represent a group of RadioButtons. Practically it is the same as your control you are using, but i think it provides better functionality and is well tested. A RadioButton is not provided by DevExpress and in my application i use the Default RadionButton-Control provided by WPF/Silverlight.

    You use the RadioListBoxEdit of DevExpress as follows:

    <dxe:ListBoxEdit SelectedItem={Binding CheckItem, Mode=TwoWay}>
      <dxe:ListBoxEdit.StyleSettings>
        <dxe:RadioListBoxEditStyleSettings />
      </dxe:ListBoxEdit.StyleSettings>
    </dxe:ListBoxEdit>
    

    More information about the ListBoxEdit of DevExpress can be found here