Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Dynamic Generation of Radio Button at Runtime using Silverlight-4 c#

In this article we are going to learn how we can create radio buttons at runtime and also we learn how to set event at runtime. When it can be used: 1. When you want to make choice we can go for Radio Button. 2. Group by Group selection is possible.

Step 1:

Create a New project

Step 2:

Select Silverlight 4 version

Step 3:

Used Namespaces:

using System.Collections.Generic; using System.Windows; using System.Windows.Controls;

Step 4:

Used Collection for Demo:

List<CompanyProduct> companyProducts = new List<CompanyProduct>(); companyProducts.Add(new CompanyProduct("Visual Studio", "MS", "MS", "MSVS")); companyProducts.Add(new CompanyProduct("SQl Server", "MS", "MS", "MSS")); companyProducts.Add(new CompanyProduct("Blend", "MS", "MS", "MSB")); companyProducts.Add(new CompanyProduct("MS Word", "MS", "MS", "MSO"));

Step 5:

Create a StatckPanel to add collection of RadioButton

StackPanel productStackPanel = new StackPanel();

Step 6:

Creating instance for RadioButton

RadioButton radioButton = new RadioButton();

Step 7:

Setting ID for RadioButton

radioButton.Name = list.Id;

Step 8:

Setting groupName for RadioButton

This allows only to select one value from the Group


//You can only select only one from group. radioButton.GroupName = list.Group;

Step 9:

Display Text for RadioButton

//Displaying Text radioButton.Content = list.Product;

Step 10:

Dynamic way of setting margin to RadioButton

radioButton.Margin = new Thickness(10.0);

Step 11:

Setting event at runtime for RadioButton

radioButton.Checked += new RoutedEventHandler(radioButton_Checked);

Step 12:

Event Method while clicking RadioButton

private void radioButton_Checked(object sender, RoutedEventArgs routedEventArgs) { string selectedContent = (string)((RadioButton)sender).Content; RadioButton radioButton = ((RadioButton) sender); radioButton.IsEnabled = false; }

Step 13:

Adding Radio Buttons to Stack Panel.

productStackPanel.Children.Add(radioButton);

Step 14:

Adding Stack Panel to Grid Root

this.LayoutRoot.Children.Add((productStackPanel));

Step 15:

Code Snippet:

namespace DynamicRadioButton { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); List<CompanyProduct> companyProducts = new List<CompanyProduct>(); companyProducts.Add(new CompanyProduct("Visual Studio", "MS", "MS", "MSVS")); companyProducts.Add(new CompanyProduct("SQl Server", "MS", "MS", "MSS")); companyProducts.Add(new CompanyProduct("Blend", "MS", "MS", "MSB")); companyProducts.Add(new CompanyProduct("MS Word", "MS", "MS", "MSO")); StackPanel productStackPanel = new StackPanel(); foreach (var list in companyProducts) { RadioButton radioButton = new RadioButton(); radioButton.Name = list.Id; //Displaying Text radioButton.Content = list.Product; //You can only select only one from group. radioButton.GroupName = list.Group; //Dynamic way of setting margin. radioButton.Margin = new Thickness(10.0); radioButton.HorizontalAlignment = HorizontalAlignment.Left; //Setting event at runtime radioButton.Checked += new RoutedEventHandler(radioButton_Checked); productStackPanel.Children.Add(radioButton); } this.LayoutRoot.Children.Add((productStackPanel)); } private void radioButton_Checked(object sender, RoutedEventArgs routedEventArgs) { string selectedContent = (string)((RadioButton)sender).Content; RadioButton radioButton = ((RadioButton) sender); radioButton.IsEnabled = false; } }

Step 16:

Output of Page Load:

Step 17:

Output of Checked Event:

Thanks for reading this article. Have a nice day.

You might also like