Xamarin Forms ListView with Check box?

Alexander Blohmé 1 Reputation point
2022-10-22T18:31:16.997+00:00

Hello, is it possible to make something like the one in image on both iOS and Android using Forms only without any renders?253166-screenshot-2022-10-22-at-202940.png

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,315 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 70,966 Reputation points Microsoft Vendor
    2022-10-24T05:32:14.493+00:00

    Hello,

    Do you want to achieve this result with Context actions without any renders?
    It cannot be achieved.

    If you want to add the checkbox to the ListView like your screenshot; if you tab the items, checkbox will be selected without renders, you can use following listview layout.

       <ListView ItemsSource="{Binding Items}" SelectionMode="Single" ItemSelected="ListView_ItemSelected">  
                   <ListView.ItemTemplate>  
                       <DataTemplate>  
                           <ViewCell>  
                               <StackLayout  Orientation="Horizontal" HorizontalOptions="FillAndExpand">  
                                   <Label Text="{Binding Name}" VerticalOptions="CenterAndExpand" HorizontalOptions="Start"></Label>  
                                   <CheckBox IsChecked="{Binding Selected}" VerticalOptions="CenterAndExpand" HorizontalOptions="EndAndExpand"></CheckBox>  
                               </StackLayout>  
                           </ViewCell>  
                       </DataTemplate>  
                   </ListView.ItemTemplate>  
               </ListView>  
    

    You can achieve the ListView_ItemSelected for selecting checkbox without render in the background code.

       private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)  
               {  
                   Model model= e.SelectedItem as Model;  
                   model.Selected =!model.Selected;  
               }  
    

    Please note, model class need to achieve INotifyPropertyChanged for runtime change like following code.

       public class Model:INotifyPropertyChanged  
           {  
               public string Name { get; set; }  
               private bool _selected;  
               public bool Selected  
               {  
                   set  
                   {  
                       if (_selected != value)  
                       {  
                           _selected = value;  
                           OnPropertyChanged("Selected");  
                           
                       }  
                   }  
                   get  
                   {  
                       return _selected;  
                   }  
               }  
         
         
         
              public event PropertyChangedEventHandler PropertyChanged;  
               protected virtual void OnPropertyChanged(string propertyName)  
               {  
                   PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));  
               }  
           }  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.