4

i want to create a checkbox in listview using xaml, after selecting the multiple checkbox click button to get all selected value.My code is :

<ContentPage.Content>
    <ListView  x:Name="ProductsListView">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <ViewCell.View>
                        <StackLayout Spacing="0">
                            <Label Text="{Binding retail_modified_item_id}" 
                               TextColor="Blue" FontSize="13" />
                            <Label Text="{Binding name, StringFormat='Name : 
                               {0:N}'}" TextColor="black" FontSize="10"  />
                            <Label Text="{Binding retail_price, 
                               StringFormat='Price : {0:N}'}" 
                               TextColor="black" FontSize="10" />
                        </StackLayout>
                    </ViewCell.View>
                </ViewCell>                            
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ContentPage.Content>

my output is; output without checkbox

i tried some sample code from https://www.ignatiuz.com/blog/xamarin/button-check-box-with-list-item-in-xamarin-forms-listview/

but i'm getting input:checkbox not found

i am new to xamarin please give some sample code or link to do this

Anu Priya
  • 87
  • 1
  • 2
  • 10

4 Answers4

2

You can use Plugin.InputKit from Nuget.

Usage

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:xxx"
             xmlns:input="clr-namespace:Plugin.InputKit.Shared.Controls;assembly=Plugin.InputKit"
             x:Class="xxx.MainPage">


<ContentPage.Content>
 <ListView  x:Name="ProductsListView">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ViewCell.View>
                    <StackLayout Spacing="0">
                        <Label Text="{Binding retail_modified_item_id}" 
                           TextColor="Blue" FontSize="13" />
                        <Label Text="{Binding name, StringFormat='Name : 
                           {0:N}'}" TextColor="black" FontSize="10"  />
                        <Label Text="{Binding retail_price, 
                           StringFormat='Price : {0:N}'}" 
                           TextColor="black" FontSize="10" />
                        <input:CheckBox Text="xxx" Type="Check"/>  
                    </StackLayout>
                </ViewCell.View>
            </ViewCell>                            
        </DataTemplate>
    </ListView.ItemTemplate>
 </ListView>
</ContentPage.Content>        

</ContentPage>
Lucas Zhang
  • 18,630
  • 3
  • 12
  • 22
1

I have written a content on implementing the multiselect listview in xamarin forms. Please check this : https://androidwithashray.blogspot.com/2018/03/multiselect-list-view-using-xamarin.html

Hope this helps.

1

Add/remove value to local collection using OnCheckChanged method. I used this way:

<ListView>
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout Orientation="Horizontal">
                    <Label Text="Some item" />
                    <CheckBox 
                        IsChecked="true" 
                        CheckedChanged="OnCheckedChanged" 
                        HorizontalOptions="EndAndExpand"/>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
John Deer
  • 2,033
  • 2
  • 13
  • 16
  • I am not a big fan of nuGet unless there is no other choice. I know this is debatable and perhaps I am wrong in this, but whatever. Thank you for this answer – Daniel Dolz Jul 21 '20 at 19:48
0

If you need to have checkboxes inside a listview, you can use Xamarin.Forms.InputKi (https://github.com/enisn/Xamarin.Forms.InputKit)

Then I would define it like this inside a DataTemplate's ViewCell:

 <input:CheckBox Text="Hello World I'm Option 2" Type="Check"/>

There are also Bindable properties and events which you can use depending on your scenario:

CheckChanged: (Event) Invokes when check changed.

CheckChangedCommand: (Command) Bindable Command, executed when check changed.

Key: (int) A key you can set to define checkboxes as ID.

Text: (string) Text to display description

IsChecked: (bool) Checkbox checked situation. Works TwoWay Binding as default.

Color: (Color) Color of selected check.

TextColor: (Color) Color of description text.

Type: (CheckType) Type of checkbox checked style. (Check,Cross,Star,Box etc.)

Amir Hajiha
  • 836
  • 8
  • 20