0

I have several groupboxes with 3 radiobuttons in each one(they are all in the grid). In WinForms I could use Controls to access the controls by name and by index like:

groupbox1.Controls[0] 

or

groupbox1.Controls["radiobutton1"]

So what I need to do in WPF is: I have a groupbox, and I need to access the certain radiobutton in it using index exactly like I would do in WinForms. Is it possible in WPF? Or how can I just reach the collection of the groupbox's children?

anako
  • 125
  • 7
  • Possible duplicate of [Getting the index of the selected RadioButton in a group](http://stackoverflow.com/questions/17082551/getting-the-index-of-the-selected-radiobutton-in-a-group) – Youngjae Mar 26 '17 at 15:28
  • sounds like this one: http://stackoverflow.com/questions/8126700/how-do-i-access-an-element-of-a-control-template-from-within-code-behind – ZSH Mar 26 '17 at 15:40

1 Answers1

0

This question is not quite a duplicate of either of the commented questions.

Let's say you have the following setup

<GroupBox>
    <StackPanel x:Name="MyRadioButtons">
        <RadioButton x:Name="radio1">Radio1</RadioButton>
        <RadioButton x:Name="radio2">Radio2</RadioButton>
        <RadioButton x:Name="radio3">Radio3</RadioButton>
    </StackPanel>
</GroupBox>

You can access the child elements of an element by index or name, then cast to RadioButton:

(MyRadioButtons.Children[0] as RadioButton)             // By index
(MyRadioButtons.FindName("radio2") as RadioButton)      // By x:Name attr
TiberiumFusion
  • 348
  • 4
  • 17