简体   繁体   中英

Check every CheckBox inside ListView

I'm new to Xamarin. I want to setup a ListView in Xamarin plus 2 buttons to check/uncheck every CheckBox inside the ListView to utilize the checked ones afterwards. Could you please tell me how to access these Checkboxes with kind of a foreach loop?

布局

<ListView x:Name="countryListView" HasUnevenRows="true" ItemTapped="Handle_ItemTapped">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <ViewCell.ContextActions>
                        <MenuItem Text="Bla" CommandParameter="{Binding .}" />
                        <MenuItem Text="Delete" IsDestructive="true" CommandParameter="{Binding .}" Clicked="deleteCountry" />
                    </ViewCell.ContextActions>
                    <StackLayout Orientation="Horizontal" Padding="5">
                        <controls:CircleImage HeightRequest="30" WidthRequest="30" Aspect="AspectFill" Source="{Binding imageURL}" VerticalOptions="Center" HorizontalOptions="Center" />
                        <!--<Image Source="{Binding imageURL}" />-->
                        <StackLayout HorizontalOptions="StartAndExpand">
                            <Label Text="{Binding name}" />
                            <Label Text="{Binding capital}" />
                        </StackLayout>
                        <CheckBox x:Name="checkbox" IsChecked="false" />
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

Thanks, Thomas

Bind the IsChecked property of your checkbox to bool in your ViewModel and in OnClick event make it true or false. Don't forget to implement INotifyPropertyChanged to refresh your UI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM