0

I hv taken 4 radiobuttons and defined one group for all. And I want to find text of selected radiobutton in that group. How to code for this. thanks

kyrylomyr
  • 12,192
  • 8
  • 52
  • 79
Anil
  • 37
  • 2
  • 7

2 Answers2

2

By modifying a bit This post you will get what you want

As that post says, add this class to your project

public static class VisualTreeEnumeration 
{ 
   public static IEnumerable<DependencyObject> Descendents(this DependencyObject root) 
   { 
     int count = VisualTreeHelper.GetChildrenCount(root); 
     for (int i=0; i < count; i++) 
     { 
       var child = VisualTreeHelper.GetChild(root, i); 
       yield return child; 
       foreach (var descendent in Descendents(child)) 
         yield return descendent; 
     } 
   } 
} 

And this will give you result you want

List<RadioButton> group = this.Descendents()
                               .OfType<RadioButton>()
                               .Where(r => r.GroupName == "aaa" && r.IsChecked == true)
                               .ToList();
Community
  • 1
  • 1
Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
  • how could any one find a selected radiobutton from group (group name is aaa) of radiobuttons – Anil Mar 14 '11 at 12:21
  • see edited post @Anil, and don't forget to mark as answer if anyone helps you, or no one will answer to your questions in the future – Arsen Mkrtchyan Mar 14 '11 at 13:54
  • Just wanted to add that for me, the radio button IsChecked property returns a nullable bool (bool?) and have to check if it is equal to true or not. I.E. myRadioButton.IsChecked == true. – Justin Jan 10 '12 at 20:25
1

From MSDN's RadioButton Class:

The following example shows two panels that contain three radio buttons each. One radio button from each panel are grouped together. The remaining two radio buttons on each panel are not grouped explicitly, which means they are grouped together since they share the same parent control. When you run this sample and select a radio button, a TextBlock displays the name of the group, or "grouped to panel" for a radio button without an explicit group name, and the name of the radio button.

XAML

<TextBlock Text="First Group:"  Margin="5" />
<RadioButton x:Name="TopButton" Margin="5" Checked="HandleCheck"
     GroupName="First Group" Content="First Choice" />
<RadioButton x:Name="MiddleButton" Margin="5" Checked="HandleCheck"
     GroupName="First Group" Content="Second Choice" />
<TextBlock Text="Ungrouped:" Margin="5" />
<RadioButton x:Name="LowerButton" Margin="5" Checked="HandleCheck"
    Content="Third Choice" />
<TextBlock x:Name="choiceTextBlock" Margin="5" />

Code Behind

private void HandleCheck(object sender, RoutedEventArgs e)
{
    RadioButton rb = sender as RadioButton;
    choiceTextBlock.Text = "You chose: " + rb.GroupName + ": " + rb.Name;
}
Metro Smurf
  • 37,266
  • 20
  • 108
  • 140