locked
RadioButton Group: Determine Selected Button RRS feed

  • Question

  • I have several radio buttons:
    ...
    <Grid Name="quickReportGrid">
      <RadioButton Name="radioButton1" GroupName="Calendar">Day</RadioButton>
      <RadioButton Name="radioButton4" GroupName="Calendar">Week</RadioButton>
      <RadioButton Name="radioButton2" GroupName="Calendar">Month</RadioButton>
      <RadioButton Name="radioButton5" GroupName="Calendar">Quarter</RadioButton>
      <RadioButton Name="radioButton3" GroupName="Calendar">Year</RadioButton>
      <Button Name="quickReportButton" Click="quickReportButton_Click">Quick Report</Button>
    </Grid>
    ...
    Is there a way to ask --GroupName="Calendar"-- what the selected RadioButton is?  Or do I have to poll each RadioButton until I find one which is selected?  What is the WPF elegant way to accomplish my goal?  What about a private RadioButton variable in my Window class that each RadioButton sets to --this-- in its Click event handler?

    Thanks,
    Dan

    Sunday, September 27, 2009 11:58 PM

Answers

  • Hi danjam,

    It's no necessary to add a separate event handler for each RadioButton, you can attach the same Checked event handler for all the RadioButtons, like follows:



     <RadioButton Name="radioButton1" GroupName="Calendar" Checked="radioButton_Checked ">Day</RadioButton>
     <RadioButton Name="radioButton4" GroupName="Calendar" Checked="radioButton_Checked ">Week</RadioButton>
     <RadioButton Name="radioButton2" GroupName="Calendar" Checked="radioButton_Checked ">Month</RadioButton>
    .....

    private void radioButton_Checked(object sender, RoutedEventArgs e)
            {
                RadioButton ck = sender as RadioButton;
                if (ck.IsChecked.Value)
                    quickReportCalendarCheckedRadioButton = ck;
            }


    Best Regards,
    Zhi-Xin


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    • Proposed as answer by Zhi-Xin Ye Thursday, October 1, 2009 7:15 AM
    • Marked as answer by Zhi-Xin Ye Monday, October 5, 2009 7:39 AM
    Monday, September 28, 2009 9:48 AM

All replies

  • OK, I went with the event driven model using the Checked event, since Web searching revealed that the RadioButton Group is useless for getting the checked RadioButton.

    - Dan

    XAML:

    <Grid>

        <RadioButton Name="dayButton" Checked="dayButton_Checked">Day</RadioButton>

        <RadioButton Name="weekButton" Checked="weekButton_Checked">Week</RadioButton>

        <RadioButton Name="monthButton" Checked="monthButton_Checked">Month</RadioButton>

        <RadioButton Name="quarterButton" Checked="quarterButton_Checked">Quarter</RadioButton>

        <RadioButton Name="yearButton" Checked="yearButton_Checked">Year</RadioButton>

        <Button Name="quickReportButton" Click="quickReportButton_Click">Quick Report</Button>

    </Grid>


    C# Code Behind:

    namespace ObjectHeaderControl1

    {

        /// <summary>

        /// Interaction logic for Window1.xaml

        /// </summary>

        public partial class Window1 : Window

        {

            private RadioButton quickReportCalendarCheckedRadioButton;

     

            public Window1()

            {

                InitializeComponent();

            }

     

            private void quickReportButton_Click(object sender, RoutedEventArgs e)

            {

                String calendarText;

                calendarText = (null == quickReportCalendarCheckedRadioButton)

                    ? "Please select a calendar unit."

                    : quickReportCalendarCheckedRadioButton.Content.ToString();

     

                MessageBox.Show(calendarText, "Quick Report");

            }

     

            private void dayButton_Checked(object sender, RoutedEventArgs e)

            {

                quickReportCalendarCheckedRadioButton = (RadioButton)sender;

            }

     

            private void weekButton_Checked(object sender, RoutedEventArgs e)

            {

                quickReportCalendarCheckedRadioButton = (RadioButton)sender;

            }

     

            private void monthButton_Checked(object sender, RoutedEventArgs e)

            {

                quickReportCalendarCheckedRadioButton = (RadioButton)sender;

            }

     

            private void quarterButton_Checked(object sender, RoutedEventArgs e)

            {

                quickReportCalendarCheckedRadioButton = (RadioButton)sender;

            }

     

            private void yearButton_Checked(object sender, RoutedEventArgs e)

            {

                quickReportCalendarCheckedRadioButton = (RadioButton)sender;

            }

         }

    }

     

    • Marked as answer by JediSQL Monday, September 28, 2009 1:04 AM
    • Unmarked as answer by JediSQL Monday, September 28, 2009 1:05 AM
    Monday, September 28, 2009 1:04 AM
  • Hi danjam,

    It's no necessary to add a separate event handler for each RadioButton, you can attach the same Checked event handler for all the RadioButtons, like follows:



     <RadioButton Name="radioButton1" GroupName="Calendar" Checked="radioButton_Checked ">Day</RadioButton>
     <RadioButton Name="radioButton4" GroupName="Calendar" Checked="radioButton_Checked ">Week</RadioButton>
     <RadioButton Name="radioButton2" GroupName="Calendar" Checked="radioButton_Checked ">Month</RadioButton>
    .....

    private void radioButton_Checked(object sender, RoutedEventArgs e)
            {
                RadioButton ck = sender as RadioButton;
                if (ck.IsChecked.Value)
                    quickReportCalendarCheckedRadioButton = ck;
            }


    Best Regards,
    Zhi-Xin


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    • Proposed as answer by Zhi-Xin Ye Thursday, October 1, 2009 7:15 AM
    • Marked as answer by Zhi-Xin Ye Monday, October 5, 2009 7:39 AM
    Monday, September 28, 2009 9:48 AM