Get Selected Item Efficiently

I have a many  Expanders with RadioGroupKeys like below in my .NET Maui app.

<StackLayout>
    <!-- Expander for Group 1 -->
    <expander:SfExpander
        x:Name="testlExpander"
        HorizontalOptions="Center"
        IsExpanded="False">
        <expander:SfExpander.Header>
            <Label
                BackgroundColor="Aqua"
                HorizontalOptions="Center"
                Text="testConstitutional" />
        </expander:SfExpander.Header>


        <expander:SfExpander.Content>
            <StackLayout>
                <StackLayout HorizontalOptions="Center" Orientation="Horizontal">
                    <Label Text="Weight Change" VerticalTextAlignment="Center" />
                    <buttons:SfRadioGroupKey
                        x:Name="weightChangeBtn1"
                        CheckedChanged="RadioGroup_CheckedChanged1"
                        Orientation="Horizontal">
                        <buttons:SfRadioButton x:Name="weightChangeBtnNA1" Text="N/A" />
                        <buttons:SfRadioButton x:Name="weightChangeBtnYes1" Text="Yes" />
                        <buttons:SfRadioButton x:Name="weightChangeBtnNo1" Text="No" />
                    </buttons:SfRadioGroupKey>
                </StackLayout>


                <StackLayout HorizontalOptions="Center" Orientation="Horizontal">
                    <Label Text="Anorexia" VerticalTextAlignment="Center" />
                    <buttons:SfRadioGroupKey
                        x:Name="anorexiaRadioGroup1"
                        CheckedChanged="RadioGroup_CheckedChanged1"
                        Orientation="Horizontal">
                        <buttons:SfRadioButton x:Name="anorexiaBtnNA1" Text="N/A" />
                        <buttons:SfRadioButton x:Name="anorexiaBtnYes1" Text="Yes" />
                        <buttons:SfRadioButton x:Name="anorexiaBtnNo1" Text="No" />
                    </buttons:SfRadioGroupKey>
                </StackLayout>


                <StackLayout HorizontalOptions="Center" Orientation="Horizontal">
                    <Label Text="Night Sweats" VerticalTextAlignment="Center" />
                    <buttons:SfRadioGroupKey
                        x:Name="nightsweatsRadioGroup1"
                        CheckedChanged="RadioGroup_CheckedChanged1"
                        Orientation="Horizontal">
                        <buttons:SfRadioButton x:Name="nightsweatsBtnNA1" Text="N/A" />
                        <buttons:SfRadioButton x:Name="nightsweatsBtnYes1" Text="Yes" />
                        <buttons:SfRadioButton x:Name="nightsweatsBtnNo1" Text="No" />
                    </buttons:SfRadioGroupKey>
                </StackLayout>


                <StackLayout HorizontalOptions="Center" Orientation="Horizontal">
                    <Label Text="Hot or Cold" VerticalTextAlignment="Center" />
                    <buttons:SfRadioGroupKey
                        x:Name="hotorcoldRadioGroup1"
                        CheckedChanged="RadioGroup_CheckedChanged1"
                        Orientation="Horizontal">
                        <buttons:SfRadioButton x:Name="hotorcoldBtnNA1" Text="N/A" />
                        <buttons:SfRadioButton x:Name="hotorcoldBtnYes1" Text="Yes" />
                        <buttons:SfRadioButton x:Name="hotorcoldBtnNo1" Text="No" />
                    </buttons:SfRadioGroupKey>
                </StackLayout>
            </StackLayout>
        </expander:SfExpander.Content>
    </expander:SfExpander>
</StackLayout>

And I want to efficiently handle the select event.  Currently this is how I'm getting my data. 

 

  private void anorexiaRadioGroup_CheckedChanged(object sender, Syncfusion.Maui.Buttons.CheckedChangedEventArgs e)
  {
      myROS.anorexia = e.CurrentItem.Text;


      System.Diagnostics.Debug.WriteLine("Anorexia Item: " + e.CurrentItem.Text);


      if (e.PreviousItem != null)


      {
          System.Diagnostics.Debug.WriteLine("Previous Item: " + e.PreviousItem.Text);
      }
  }


  private void nightsweatsRadioGroup_CheckedChanged(object sender, Syncfusion.Maui.Buttons.CheckedChangedEventArgs e)
  {
      myROS.night_sweats = e.CurrentItem.Text;


      System.Diagnostics.Debug.WriteLine("Night Sweats Item: " + e.CurrentItem.Text);


      if (e.PreviousItem != null)


      {
          System.Diagnostics.Debug.WriteLine("Previous Item: " + e.PreviousItem.Text);
      }
  }

How can I use a switch statement inside of the SelectionEvent and get the e.CurrentItem.Text to save to my db?




1 Reply

KJ Kishore Jeyachandran Syncfusion Team March 11, 2025 02:00 PM UTC

Hi Frederick,
 

Thank you for contacting Syncfusion support.


Based on your requirement to efficiently handle multiple radio groups with a single event handler, here's a solution using a switch statement with pattern matching for reference equality. The below code identify the radio group by name or identifier, which makes the solution more maintainable as your application grows.

Code Snippet :
 

private void RadioGroup_CheckedChanged1(object sender, Syncfusion.Maui.Buttons.CheckedChangedEventArgs e)

 {

 

     string selectedValue = e.CurrentItem?.Text;

 

     if (selectedValue == null)

         return;

 

     switch (sender)

     {

         case var s when s == weightChangeBtn1:

             myROS.weight_change = selectedValue;

             System.Diagnostics.Debug.WriteLine($"Weight Change: {selectedValue}");

             break;

 

         case var s when s == anorexiaRadioGroup1:

             myROS.anorexia = selectedValue;

             System.Diagnostics.Debug.WriteLine($"Anorexia: {selectedValue}");

             break;

 

         case var s when s == nightsweatsRadioGroup1:

             myROS.night_sweats = selectedValue;

             System.Diagnostics.Debug.WriteLine($"Night Sweats: {selectedValue}");

             break;

 

         case var s when s == hotorcoldRadioGroup1:

             myROS.hot_or_cold = selectedValue;

             System.Diagnostics.Debug.WriteLine($"Hot or Cold: {selectedValue}");

             break;

 

         default:

             System.Diagnostics.Debug.WriteLine($"Unhandled radio group, Value: {selectedValue}");

             break;

     }

 

 

     if (e.PreviousItem != null)

     {

         System.Diagnostics.Debug.WriteLine($"Previous value: {e.PreviousItem.Text}");

     }

 }


This approach will handle multiple radio groups with single event handler using switch.


If you have any questions or need further assistance, please don't hesitate to reach out!


Regards,
Kishore J


Loader.
Up arrow icon