locked
How to create radiobuttons dynamically by code?

    Question

  • Hi!

    How do I create a few radiobuttons dynamically on to a page by C# code ? I got a list of Languages the user can select from - "nb", "en-US", "sp-ES" and would like to make a radiobutton for each inside the code below.

    I also would like to get some tip how to get hold of the selected radiobutton and it's value.

    Thanks in advance!

    <StackPanel x:Name="MainStackPanel" VerticalAlignment="Center">
    </StackPanel>


    Sigurd F

    Wednesday, February 4, 2015 5:24 PM

Answers

  • Hi Sigurd,

    Per my understanding, you want to get resource according the current language from string resource or file resource. Here is a MSDN code sample shows how to read string from resource at runtime. https://code.msdn.microsoft.com/windowsapps/Application-resources-and-cd0c6eaa. You can refer to see Scenario 8 in the above sample. Code looks like the following.

    ResourceContext defaultContextForCurrentView = ResourceContext.GetForCurrentView();
    
                ResourceMap stringResourcesResourceMap = ResourceManager.Current.MainResourceMap.GetSubtree("Resources");
    
                RadioButton radioButton1 = new RadioButton();
    
                radioButton1.Content = stringResourcesResourceMap.GetValue("radiobuttontitle", defaultContextForCurrentView).ValueAsString;
    
                radioButton1.GroupName = stringResourcesResourceMap.GetValue("radiobuttongroup", defaultContextForCurrentView).ValueAsString;
    
                radioButton1.Checked += RadioButton_Checked;
    

    About your second question, what do you mean about “get hold of the selected radiobutton”? Can you explain more? Do you just need to get the selected radiobutton value checked by user? Just checking the checked property in radiobutton class. Something more about radiobutton. https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.radiobutton.aspx.

    Feel free to let me know if I have any misunderstanding.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    • Marked as answer by Sigurd F Tuesday, February 10, 2015 10:19 AM
    Thursday, February 5, 2015 6:52 AM
    Moderator

All replies

  • Hi Sigurd,

    Per my understanding, you want to get resource according the current language from string resource or file resource. Here is a MSDN code sample shows how to read string from resource at runtime. https://code.msdn.microsoft.com/windowsapps/Application-resources-and-cd0c6eaa. You can refer to see Scenario 8 in the above sample. Code looks like the following.

    ResourceContext defaultContextForCurrentView = ResourceContext.GetForCurrentView();
    
                ResourceMap stringResourcesResourceMap = ResourceManager.Current.MainResourceMap.GetSubtree("Resources");
    
                RadioButton radioButton1 = new RadioButton();
    
                radioButton1.Content = stringResourcesResourceMap.GetValue("radiobuttontitle", defaultContextForCurrentView).ValueAsString;
    
                radioButton1.GroupName = stringResourcesResourceMap.GetValue("radiobuttongroup", defaultContextForCurrentView).ValueAsString;
    
                radioButton1.Checked += RadioButton_Checked;
    

    About your second question, what do you mean about “get hold of the selected radiobutton”? Can you explain more? Do you just need to get the selected radiobutton value checked by user? Just checking the checked property in radiobutton class. Something more about radiobutton. https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.radiobutton.aspx.

    Feel free to let me know if I have any misunderstanding.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    • Marked as answer by Sigurd F Tuesday, February 10, 2015 10:19 AM
    Thursday, February 5, 2015 6:52 AM
    Moderator
  • What I actually wanted was creating RadioButtons dynamically. Something like the code below.
    And for my second answer - I thought that I had to check each radioButtons to see which was selected. But I found out that I could just check the selected RadioButton.

            /// <summary>
            /// Just for testing 
            /// </summary>
            private void Testing(string SelectedLanguage)
            {
                List<String> lstLanguage = new List<String>(new string[] { "nb", "en-US", "sp-ES" });
                foreach(string item in lstLanguage) {
                    RadioButton radioButton = new RadioButton();
                    radioButton.Name = item;
                    radioButton.Content = item;
                    radioButton.GroupName = "Group1";
                    radioButton.Checked += RadioButton_Checked;
                    if (item == SelectedLanguage)
                        radioButton.IsChecked = true;
    
                    MainStackPanel.Children.Add(radioButton);
                }
            }
    
    Regards, Sigurd F

    Tuesday, February 10, 2015 10:19 AM