When it can be used:
- When you want to make a choice we can use a Radio Button.
- 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 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;



Join the conversation! Your thoughts help the community grow.