Dynamic Generation of RadioButton at Runtime Using Silverlight 4 C#


When it can be used:

  1. When you want to make a choice we can use a Radio Button.
  2. Group by Group selection is possible.

Step 1: Create a New project:

radiobutton.gif

Step 2: Select Silverlight 4 version:

radiobutton1.gif

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 a collection of RadioButtons:

StackPanel productStackPanel = new StackPanel();

Step 6: Creating instance of a RadioButton:

RadioButton radioButton = new RadioButton();

Step 7: Setting ID for RadioButton:

radioButton.Name = list.Id;

Step 8: Setting groupName for RadioButton:

This allows selection of only one value from the Group:

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

Step 9: Display Text for the RadioButton:

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

Step 10: Dynamic way of setting a margin to RadioButton:

radioButton.Margin =
new Thickness(10.0);

Step 11: Setting an 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 a Stack Panel:

productStackPanel.Children.Add(radioButton);

Step 14: Adding a Stack Panel to a 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:

radiobutton2.gif

Step 17: Output of Checked Event:

radiobutton3.gif

Thanks for reading this article. Have a nice day.


Similar Articles