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:
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?
CodePudding user response:
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);