0

I have a group of radiobuttons (about 20) placed in different expanders.

 <StackPanel>
    <Expander>
        <Expander.Header>
            <TextBlock>Expander1</TextBlock>
        </Expander.Header>
        <StackPanel>
            <RadioButton GroupName="G" x:Name="RadioButton1">Radiobutton1</RadioButton>
            <RadioButton GroupName="G" x:Name="RadioButton2">Radiobutton2</RadioButton>
        </StackPanel>
    </Expander>
    <Expander>
        <Expander.Header>
            <TextBlock>Expander2</TextBlock>
        </Expander.Header>
        <StackPanel>
            <RadioButton GroupName="G" x:Name="RadioButton3">Radiobutton3</RadioButton>
            <RadioButton GroupName="G" x:Name="RadioButton4">Radiobutton4</RadioButton>
        </StackPanel>
    </Expander>
    <!-- ... -->
</StackPanel>

I have user controls in different xaml files and a MainWindow.xaml. Depending on a checked radiobutton a specific user control needs to be added to the main window. What is the best way to handle "Checked" event for these 20 radiobuttons? How to know which radiobutton is checked?

katzenklo
  • 1
  • 1
  • Are you using MVVM? – Yevgeniy May 19 '17 at 14:21
  • If you are using MVVM (which you probably should be) take a look at the Command Interface. I use RelayCommand personally. – Tim Hutchison May 19 '17 at 14:33
  • If you are using MVVM, bind the IsChecked property of each RadionButton to an enum. This topic can help you - http://stackoverflow.com/questions/397556/how-to-bind-radiobuttons-to-an-enum – Yevgeniy May 19 '17 at 15:06

1 Answers1

0

Use the Checked event.

Each RadioButton can have its own Checked event handler or if they share a common handler then you can use the first parameter (sender object) like so:

private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
    RadioButton yourControl = sender as RadioButton;
}
Leo
  • 5,013
  • 1
  • 28
  • 65