WPF RadiobuttonList in WPF

Introduction

By default, WPF does not support Radio button list box like old Win Form Radio Button List. Here this blog describes how to create custom control or Radio List Box in WPF with help of XAML Code.

Getting Started

As we know that WPF does not have Radio Button List Box like Win form. But it can be created by customizing the existing List Box control with help of the item template of List Box control, data template for binding, and Radio Button.

WPF provides Template for customizing the structure and appearance of existing control the same as the Listbox has an item template where you can change the structure of items to be displayed inside the list box. The item control of List box normally used place control to display control like text box, button etc. as an item of Listbox. hence here I have used the ListBox item template to display the radio button in the site list box item.

For displaying data or text in an item, the data template of WPF is used here. Data Template is used to display the content of control or customize the data inside the controls. A Data Template to specify the visualization of your data objects. Data Template objects are particularly useful when you are binding an Items Control such as a List Box to an entire collection. Without specific instructions, a List Box displays the string representation of the objects in a collection. In that case, you can use a Data Template to define the appearance of your data objects. The content of your Data Template becomes the visual structure of your data objects.

The below code example creates a custom radio button list box control, it supports all the features like WinForm RadioButton List has and WPF feature like binding. This example lists out students on the list and displays the name of students with the radio button.

XAML Code:

  <ListBox Name="listBoxZone" >  
       <ListBox.ItemTemplate>  
         <DataTemplate>  
           <RadioButton Content="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}" IsChecked="{Binding Path=IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"></RadioButton>  
         </DataTemplate>  
       </ListBox.ItemTemplate>  
     </ListBox>  

C#Code:

 private void FillList()  
     {  
       Students.Add(new Student() { Name = "Student1", IsSelected = false });  
       Students.Add(new Student() { Name = "Student2", IsSelected = false });  
       Students.Add(new Student() { Name = "Student3", IsSelected = false });  
       Students.Add(new Student() { Name = "Student4", IsSelected = false });  
       Students.Add(new Student() { Name = "Student5", IsSelected = false });  
       this.listBoxZone.ItemsSource = this.Students;  
     }  

Related Articles

  1. Data Validation in WPF
  2. WPF Round Corner Button with click effects
  3. Round Corner PasswordBox in WPF
  4. Round Corner TextBox in WPF
  5. WPF Custom Datagrid Control(Filterable)
  6. WPF Round Corner ListBox
  7. Custom RadioButtonListBox With Image
  8. RadiobuttonList in WPF
  9. Custom CheckedListBox in WPF

Thanks

Kailash Chandra Behera

An IT Professional with 12 years experience in development life cycle in windows, service and Web based application using Microsoft.Net technologies. Proven record of developing all phases of projects in Microsoft.Net technology from initiation to closure aligning with the company's Business objectives to drive process improvements, competitive advantage and bottom-line gains. -> Good exposure of independently working and developing multiple projects ->Committed to efficient and effective development of projects in a fast-paced and deadline driver environment. Skill :- Develop and design projects in various technologies of Microsoft Technology. Total IT Experience- 13+

Previous Post Next Post

نموذج الاتصال