Click here to Skip to main content
15,902,635 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I'm very new to Silverlight & C# and I need help with following:

From XML data I'm creating as many RadioButtons as needed (as many as there are root.nodes) as following:

C#
foreach (var vItem in vXDocument.Descendants("kaart"))
            {
                vItemsCount++;

                // MC1
                RadioButton vRadioButton_MC1 = new RadioButton();
                vRadioButton_MC1.Content = vItem.Element("MC1").Value;
                vRadioButton_MC1.Name = vItemsCount + "-MC1";
                vListBox.Items.Add(vRadioButton_MC1);
 
                // MC2
                RadioButton vRadioButton_MC2 = new RadioButton();
                vRadioButton_MC2.Content = vItem.Element("MC2").Value;
                vRadioButton_MC2.Name = vItemsCount + "-MC2";
                vListBox.Items.Add(vRadioButton_MC2);
            };


Now I want to check with RadioButtons are checked and with aren't. Every object has his own specified name (vRadioButton_MC1.Name = vItemsCount + "-MC1"). Can I ask this property for each RadioButtons?
Thanks for paying attention.


[edit]Additional information by OP in a non-solution moved here [/edit]
made an user control in xaml, wich made it easy to set a variable when a radiobutton is checked or not. That variable i've send to my code behind to work with.
Posted
Updated 1-May-12 8:22am
v2

There are two general ways of handling this depending on wether you are using MVVM pattern or not. If you are not using MVVM, then you can use the OnChecked event which includes a sender argument. The value entered can be used to determine the radio button that was changed:

C#
private void OnChecked(object sender, RoutedEventArgs e)
{
    var control = (RadioButton)sender;
}


To use this you need to add the event to the control:

vRadioButton_MC1.Checked += OnChecked;


The better way to do this is to use the MVVM pattern, but that will be much more complex since you have to create a ICommand binding in code. The CommandParameter would be u8sed to determine which RadioButton was activated.
 
Share this answer
 
You can write the code like:
C#
vRadioButton.IsChecked

IsChecked property is refereed at http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.togglebutton.ischecked%28v=VS.95%29.aspx[^]
 
Share this answer
 
Comments
Johan DC 3-Jan-12 10:07am    
in case of 100 RadioButtons for MC1, it's possible that few are checked and others unchecked. So I need for example 1-MC1 (= vRadioButton_MC1.Name) is checked, 2-MC2...
made an user control in xaml, wich made it easy to set a variable when a radiobutton is checked or not. That variable i've send to my code behind to work with.
 
Share this answer
 
Comments
Nelek 1-May-12 14:23pm    
Please use "improve question" or "have a question or comment" to add information to your problem or to speak with people answering you. But don't add solutions

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900