Click here to Skip to main content
15,115,557 members
Articles / Desktop Programming / WPF
Posted 6 Sep 2009


23 bookmarked

RadioListBox: A ListBox with Radio Buttons (WPF Version)

Rate me:
Please Sign up or sign in to vote.
4.64/5 (7 votes)
6 Sep 2009CPOL2 min read
How to implement a templated ListBox with radio buttons instead of the standard selection highlight.




This is the WPF version of my previous articles: CRadioListBox: A ListBox with Radio Buttons (MFC Version) and RadioListBox: A ListBox with Radio Buttons (WinForms Version). As explained in those articles, RadioListBox has the same functionalities as a regular ListBox, but with a different look and feel. Anyway, the RadioListBox control offers some advantages:

  • It is clearer that options are mutually exclusive with radio buttons.
  • It is a good alternative to a group of radio buttons because you have to maintain just one control, less memory use.
  • It inherits some useful features like scrolling, sorting, data binding, and multi-column.
  • It will be easier to change options dynamically, as shown in the demo application.
  • It will be easier to manage selection events, also shown in the demo application.

Using the Code

To implement RadioListBox into your project, you just need to do a few steps:

  • Include RadioListBox.xaml and RadioListBox.xaml.cs into your project.
  • Drop a RadioListBox object into your XAML form, or do it by hand. An alias for the System.Windows.Controls.Custom namespace will be useful.
  • Change the standard properties of the control, just like for a ListBox.
  • Use the IsTransparent property to mimic a group of RadioButtons.

That's all! Now you can use the radio button collection as a regular ListBox. You can add items with the Items.Add() method, the ItemsSource property, or directly into XAML. Finally, query for user selection with the SelectedIndex property. No more one-by-one evaluations.

RadioListBox Internals

The RadioListBox class is derived from WPF's ListBox class with a custom ControlTemplate to remove all undesired ListBox functionalities like selected item's background. To be consistent with the radio button model, multiple selection has been disabled. The resumed XAML implementation is the following:

<ListBox x:Class="System.Windows.Controls.Custom.RadioListBox"
       xmlns:s="clr-namespace:System;assembly=mscorlib" >
        <Style x:Key="{x:Type ListBoxItem}" TargetType="ListBoxItem">
            <Setter Property="SnapsToDevicePixels" Value="true"/>
            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Setter Property="Template">
                    <ControlTemplate TargetType="ListBoxItem">
                        <RadioButton x:Name="radio" Click="ItemRadioClick">
                                <ContentPresenter ... />
            <Border BorderThickness="0" 
                <ScrollViewer Padding="{TemplateBinding Control.Padding}" 
                      SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
            <ControlTemplate.Triggers ... />

As the radio buttons inside the ControlTemplate don't change the selection, this has to be done manually inside the ItemRadioClick event. Here, the ItemContainerGenerator property has an important role by converting between the ListBox's items contents and the ListBoxItem presentation object. Here is a summarized C# source code:

namespace System.Windows.Controls.Custom
    public partial class RadioListBox : ListBox
        public RadioListBox() { ... }

        public new SelectionMode SelectionMode { ... }

        public bool IsTransparent { ... }

        private void ItemRadioClick(object sender, RoutedEventArgs e)
            ListBoxItem sel = (e.Source as RadioButton).TemplatedParent as ListBoxItem;
            int newIndex = this.ItemContainerGenerator.IndexFromContainer(sel); ;
            this.SelectedIndex = newIndex;

        protected override void OnSelectionChanged(SelectionChangedEventArgs e)

            CheckRadioButtons(e.RemovedItems, false);
            CheckRadioButtons(e.AddedItems, true);

        private void CheckRadioButtons(System.Collections.IList radioButtons, bool isChecked) 
            foreach (object item in radioButtons)
                ListBoxItem lbi = 
                  this.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;

                if (lbi != null)
                    RadioButton radio = lbi.Template.FindName("radio", lbi) as RadioButton;
                    if (radio != null)
                        radio.IsChecked = isChecked;


  • September 6th, 2009: First version.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jaime Olivares
Peru Peru

Computer Electronics professional, Software Architect and senior Windows C++ and C# developer with experience in many other programming languages, platforms and application areas including communications, simulation systems, PACS/DICOM (radiology), GIS, 3D graphics and HTML5-based web applications.
Currently intensively working with Visual Studio and TFS.

Comments and Discussions

QuestionChange Radio button Style Pin
Supa Spark Wang25-Nov-15 0:45
MemberSupa Spark Wang25-Nov-15 0:45 
QuestionBy Default First RadioButton Selected? Pin
Member 1121301011-Mar-15 4:52
MemberMember 1121301011-Mar-15 4:52 
AnswerRe: By Default First RadioButton Selected? Pin
Jaime Olivares13-Jul-15 14:59
MemberJaime Olivares13-Jul-15 14:59 
QuestionException Occurred while Binding Pin
MAQAH29-May-14 22:04
MemberMAQAH29-May-14 22:04 
AnswerRe: Exception Occurred while Binding Pin
MAQAH4-Jun-14 20:25
MemberMAQAH4-Jun-14 20:25 
GeneralMy vote of 5 Pin
Shailesh vora30-Sep-12 1:19
MemberShailesh vora30-Sep-12 1:19 
GeneralBind a DataTable Pin
MCTimotheus4-Feb-10 2:48
MemberMCTimotheus4-Feb-10 2:48 
GeneralRe: Bind a DataTable Pin
Jaime Olivares7-Feb-10 19:03
MemberJaime Olivares7-Feb-10 19:03 
GeneralRe: Bind a DataTable Pin
MCTimotheus7-Feb-10 21:36
MemberMCTimotheus7-Feb-10 21:36 
Generala loop "for" Pin
peug30-Jan-10 7:23
Memberpeug30-Jan-10 7:23 
GeneralRe: a loop "for" Pin
Jaime Olivares31-Jan-10 7:25
MemberJaime Olivares31-Jan-10 7:25 
JokeRe: a loop "for" Pin
peug31-Jan-10 7:27
Memberpeug31-Jan-10 7:27 
GeneralListBox with checkbox Pin
peug29-Jan-10 3:03
Memberpeug29-Jan-10 3:03 
GeneralRe: ListBox with checkbox Pin
Jaime Olivares31-Jan-10 7:30
MemberJaime Olivares31-Jan-10 7:30 
QuestionSpace between radio Pin
peug29-Jan-10 1:09
Memberpeug29-Jan-10 1:09 
AnswerRe: Space between radio Pin
peug31-Jan-10 7:28
Memberpeug31-Jan-10 7:28 
GeneralRe: Space between radio Pin
Jaime Olivares31-Jan-10 7:32
MemberJaime Olivares31-Jan-10 7:32 
GeneralBug Pin
naish_kewl30-Nov-09 15:41
Membernaish_kewl30-Nov-09 15:41 
GeneralRe: Bug Pin
Jaime Olivares1-Dec-09 3:20
MemberJaime Olivares1-Dec-09 3:20 
GeneralRe: Bug Pin
naish_kewl3-Feb-10 12:51
Membernaish_kewl3-Feb-10 12:51 
GeneralRe: Bug Pin
Jaime Olivares7-Feb-10 19:25
MemberJaime Olivares7-Feb-10 19:25 
GeneralRe: Bug Pin
naish_kewl8-Feb-10 13:07
Membernaish_kewl8-Feb-10 13:07 
GeneralRe: Bug Pin
Jaime Olivares8-Feb-10 13:14
MemberJaime Olivares8-Feb-10 13:14 
GeneralRe: Bug Pin
Member 83632665-Apr-12 11:25
MemberMember 83632665-Apr-12 11:25 
General"Checking" in XAML is better Pin
Guillaume Leparmentier6-Sep-09 14:57
MemberGuillaume Leparmentier6-Sep-09 14:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.