Thanks a lot for your reply John,
I tried what you suggested and it has worked for me. Below is the code I have come up with
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private static Dictionary<string, List<string>> myQuestions = new Dictionary<string, List<string>>();
private static Dictionary<string, List<RadioButton>> myRadioButtonControls = new Dictionary<string, List<RadioButton>>();
public MainWindow()
{
InitializeComponent();
FillQuestions();
CreateControls(myStackPanel);
}
private static void FillQuestions()
{
myQuestions.Add("Favourite fruit", new List<string>() { "Apple", "Orange", "Grape" });
myQuestions.Add("Favourire vegitable", new List<string>() { "Tomato", "Potato"});
myQuestions.Add("Favoutite Animal", new List<string>() { "Lion", "Bear", "Elephant","Cow" });
}
private static void CreateControls(StackPanel s)
{
foreach (KeyValuePair<string,List<string>> item in myQuestions)
{
myRadioButtonControls.Add(item.Key, new List<RadioButton>());
foreach (string a in item.Value)
{
((List<RadioButton>)(myRadioButtonControls[item.Key])).Add(new RadioButton() { Content = a, GroupName = item.Key});
}
}
foreach (KeyValuePair<string,List<RadioButton>> item in myRadioButtonControls)
{
s.Children.Add(new TextBlock() { Text= item.Key });
foreach (RadioButton r in item.Value)
{
s.Children.Add(r);
}
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
StringBuilder s = new StringBuilder();
foreach (KeyValuePair<string, List<RadioButton>> item in myRadioButtonControls)
{
s.Append(string.Format("\nQ. {0}", item.Key));
foreach (RadioButton r in item.Value)
{
if ((bool)r.IsChecked)
{
s.Append(string.Format("\nA. {0}\n",r.Content));
}
}
}
MessageBox.Show(s.ToString());
}
}
}
Could you kindly let me know if I am doing things right? Also I would highly appreciate any suggestions to improve above code.
On a related note, how do I go about rapping each Quesion,Answer Set in a Group box?
Many Thanks again for your help..