2

I am working on silverlight and i have created a radiobutton i want to set manually the check of radiobutton programatically to the given 3 items (itms means 3 radiobutton) on radio button.I tried like this

RadioButton radio = new RadioButton();

suppose this radio buttons contains " items( radio buttons) and when i try the code below to check the second item (out of 3 items) the code below checks the last item.

radio.Loaded += (p, q) =>
{       
       radio.IsChecked = true;
};

How i create button is :

foreach (String item in param.Component.Attributes[0].Item)
            {
                radio = new RadioButton()
                {
                    Content = item,
                    GroupName = "MyRadioButtonGroup",
                    Tag = tg

                };
 radio.Checked += (o, e) =>
                {
                    //Do  something                
                };

                sp.Children.Add(radio);
                count++; tg++;              
            }

Why checks the last item ? How to check the second item programatically usin silverlight in c# code? I try to do so because i want to intialise the radio button after the program launch (before button clicks) because on before button click of radio buttons if i try to print the itmes on text block, I see nothing (it only show on button clicks but i want something displayed before clicking instead of empty space)

Sss
  • 1,519
  • 8
  • 37
  • 67
  • Unless I'm mistaken (in which case - my apologies), you will need a postback to happen in order for the radio button to change visually, as you change the "IsChecked" value after it's already initialized. PS.: It's much simpler to do it via jQuery, if you're willing to use it. – EvilBeer Jun 23 '14 at 16:03
  • @EvilBeer please see the edit why i try to do so . – Sss Jun 23 '14 at 16:06
  • are you allowed/willing to use jQuery/Javascript? – EvilBeer Jun 23 '14 at 16:32
  • Are you creating the Radiobuttons in Code or in XAML? – Walt Ritscher Jun 23 '14 at 18:26
  • @EvilBeer there is nothing like Java ..Only C#. – Sss Jun 23 '14 at 21:43
  • @WaltRitscher No i have created buttons programatically and assign them items through the object obtained i deserializing that xml. – Sss Jun 23 '14 at 21:45
  • @user234839 Can you post a little bit more of your code? I'd like to see how you're creating all three radio buttons and adding them to the parent. – Markus Jun 24 '14 at 03:19
  • @Markus please see th edit. – Sss Jun 24 '14 at 07:20
  • That's what I thought. The reason you get the last item checked is because setting any of the radio button's IsChecked properties to true sets all of the other radio button's IsChecked properties to false. Since you're setting it in the Load event for each radio button, the last one wins. See @WaltRitscher 's answer and that should help, thought I agree with his comment that the solution is not taking advantage of the power of binding in WPF. – Markus Jun 25 '14 at 02:47

1 Answers1

2

The short answer to your question is that you set radio.IsChecked=truein the code to make the RadioButton checked.

One way to solve this is to set IsChecked = true when the second radio button is added. Since your code is in a foreach loop your count variable should work.

   foreach (String item in param.Component.Attributes[0].Item)
            {
                radio = new RadioButton()
                {
                    Content = item,
                    GroupName = "MyRadioButtonGroup",
                    Tag = tg

                };
 radio.Checked += (o, e) =>
                {
                    //Do  something                
                };

                sp.Children.Add(radio);
                count++; tg++;   
                if (count == 2){
                radio.IsChecked=true;
                }
            }

Or you could change your foreach loop to a for loop

for (int i = 0; i < param.Component.Attributes[0].Item.Count ; i++)
      {

      }


if (i== 2){
     radio.IsChecked=true;
   }

While both these approaches work they don't mesh well with the databound approach common in most Silverlight applications. The initial state of the selected items should really be stored in a state variable.

Walt Ritscher
  • 6,977
  • 1
  • 28
  • 35