How to bind listview with checkbox in xamarin

anil kumar 61 Reputation points
2022-10-18T08:27:16.473+00:00

I want to bind listview with data from table and checkbox in each row
after that selected items using checkbox in listview should be update record in table on button click event
can someone have any idea with demo. My sample is as below

<StackLayout>
<ListView IsVisible="true" x:Name="ListView" RowHeight="50" ItemSelected="ListView_ItemSelected" CachingStrategy="RecycleElement" SeparatorColor="Transparent" BackgroundColor="Transparent">

               <ListView.ItemTemplate>  

                   <DataTemplate>  

                       <ViewCell>  
                        <StackLayout HorizontalOptions="StartAndExpand"  VerticalOptions="Center">  
                              
                            <Label Text="{Binding Amount}"   FontSize="11" TextColor="Black"  
                                                HorizontalTextAlignment="Start"/>  
                            <Label Text="{Binding Date}"   FontSize="11" TextColor="Black"  
                                                HorizontalTextAlignment="Start"/>  
                            <input:CheckBox  HorizontalOptions="CenterAndExpand" IsChecked="{Binding ROWCHECK , Mode=TwoWay}" Type="Check" CheckChanged="CheckBox_CheckChanged"/>  
                             
                        </StackLayout>  
                           
            </ViewCell>  

            </DataTemplate>  

            </ListView.ItemTemplate>  

        </ListView>  
        <StackLayout>  

            <Button Text="Done" TextColor="White" HeightRequest="62" WidthRequest="201" Clicked="SelectItems" VerticalOptions="EndAndExpand" HorizontalOptions="CenterAndExpand" BackgroundColor="#3DBDA0" Margin="0,0,0,62"/>  

        </StackLayout>  
    </StackLayout>  

My model class
public DateTime Date { get; set; }
public Decimal Amount { get; set; }
public String office{ get; set; }
public String user{ get; set; }
public bool ROWCHECK { get; set; }

Iteration to find value i am using is here.
IncomeCollection = new ObservableCollection<BranchViewModal>();

        for (int i = 0; i < IncomeCollection.Count; i++)  
        {  
            BranchViewModal item = IncomeCollection[i];  

            if (item.ROWCHECK)  
            {  
                IncomeCollection.Add(item);  
            }  
        }  

in this I want to select items and when click on button than record is to be updated in table as per row index
Thanks in advance
AKY

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

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 37,421 Reputation points Microsoft Vendor
    2022-10-19T03:02:57.897+00:00

    Hello,

    It is necessary to add ItemSouce into ListView as the following code to bind data, please refer to ListView Data Sources.

       <ListView IsVisible="true" x:Name="ListView" ItemSource="{Binding IncomeCollection}">  
       ...  
       </ListView>  
    

    As a matter of fact, binding bool value to an item is the same as binding other properties on Xamarin.Forms. This official ListView sample would be helpful for you: ListView.

    In addition, it is recommended to use Command instead of Event on MVVM style. Please refer to UI Interaction using Commands and Behaviors to get more details about what is MVVM and how to use Command in Xamarin.Forms.

    Best Regards,

    Alec Liu.


    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.