Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get selected CheckBoxes Value/Name in .NET MAUI

I have a dynamic checkbox,

private async void GetOffices()
{
    string url = $"{baseurl}/get-offices";

    var response = await httpservices.SendGetRequest(url);

    for (int i = 0; i < response.Count; i++)
    {
        Offices.Add(new Office() { id = (string)response[i]["id"], OfficeName = (string)response[i]["office_name"] });
    }
}

And my XAML looks like this:

<CollectionView ItemsSource="{Binding Offices}">
    <CollectionView.ItemTemplate>
        <DataTemplate x:DataType="viewmodel:Office" >
            <Grid Padding="5">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <Frame Grid.ColumnSpan="2" Padding="15" BackgroundColor="LightBlue" x:Name="OfficeSelection" CornerRadius="0">
                    <Grid Grid.ColumnSpan="2" RowDefinitions="Auto,Auto" ColumnDefinitions="40,*">

                        <CheckBox Grid.Column="0" x:Name="{Binding OfficeName}" CheckedChanged="OnCheckBoxCheckedChanged"/>
                        <Label TextTransform="Uppercase" Padding="10" FontSize="Default" Text="{Binding OfficeName}" Grid.Column="1"></Label>

                    </Grid>
                </Frame>

            </Grid>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

And it will display like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

What I want to do is, when I click the continue button, I can get all the checked checkboxes Name or value where in this case are: ADMISSION, CASHIER, PLENARY HALL and store it in array?

This is my button XAML:

<Button
Grid.Row="1"
Grid.Column="0"
Margin="5"
BorderWidth="0"
Command="{Binding GetSelectedOfficesCommand}"
CornerRadius="0"
Text="Continue" />

[RelayCommand]
public void GetSelectedOffices()
{
  //CODe HERE     
       
}

MAUI related MS Docs is quiet confusing for me. Anyone can give me a code snippet to solve this?

>Solution :

You should add a boolean property to the office view model and bind it to the checkbox IsChecked. Then in your GetSelectedOffices function you just filter by this property:

var selectedNames = Offices.Where(x => x.IsSelected).Select(x => x.OfficeName);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading