I tried something like:
RadioButton[] diff = new RadioButton[10];
for (int i = 0; i < 10; ++i)
{
diff[i] = (RadioButton)Control("rad_D" + i.ToString());
}
Clearly does not work. But what was the correct way?
I tried something like:
RadioButton[] diff = new RadioButton[10];
for (int i = 0; i < 10; ++i)
{
diff[i] = (RadioButton)Control("rad_D" + i.ToString());
}
Clearly does not work. But what was the correct way?
Edit: Ok, so you are looking for already existing RadioButtons, in that case, use FindName. Something like this:
RadioButton[] diff = new RadioButton[10];
for (int i = 0; i < 10; ++i)
{
diff[i] = someparentControl.FindName("rad_D" + i.ToString()) as RadioButton;
}
You need a parent control, which can just be the window itself as it will search recursively for a matching name.
Try this, if the radio buttons are direct children of a Panel such as a Grid, StackPanel etc then you can do this:
var buttons = grid.Children.OfType<RadioButton>().
Where(rb => rb.Name.StartsWith("rad_D").ToList();
to obtain a List, or if you really want an array
var buttons = grid.Children.OfType<RadioButton>().
Where(rb => rb.Name.StartsWith("rad_D").ToArray();
And you can use this article Find all controls in WPF Window by type which has several methods for finding all controls of a specific type.