How to access a control inside the listview

How to check whether the checkbox control has been ticked or not inside the listview control while clicking button control?. pls share me if you have any samples?

Best Answer

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    CheckBox? In XF there is not a CheckBox control.

    BTW, it should be in binding with a property in your Model. Check the property value.

  • Sumit_SharmaSumit_Sharma USMember ✭✭✭

    Instead of checkbox you can use Switch Control.

    Sample is also available

    Please refer https://developer.xamarin.com/recipes/cross-platform/xamarin-forms/controls/multiselect/

  • Mani787Mani787 INMember ✭✭✭

    Thank you AlessandroCaliaro and sumit_sharma.

    Based on your input i have binded the data using property in my model. But i want to know, how to get the selected item values in button click on the same screen, not the toolbar click event. kindly guide me and share me the sample if you are having it.

  • Mani787Mani787 INMember ✭✭✭

    Dear AlessandroCaliaro and sumit_sharma
    Actually, i am having list view control with checkbox and button control(outside of list view) in one page. On button click event, i need to get the selected checkbox item values and sent to database. how to get the selected checkbox item value in button click. share me any samples if you are having it.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I think you have a Model that has a Selected Property. This Selected property should be in Binding with your CheckBox (Or Switch control).

    When you press the Button, you have to take, from your ObservableCollection, all items with Selected == true.

    Which is the problem?

  • Mani787Mani787 INMember ✭✭✭

    how to access the observablecollection in button control. actually i dont know how to access.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    You should post your code. Where do you define your ObservableCollection? In CodeBehind? Do you use MVVM?

  • Mani787Mani787 INMember ✭✭✭

    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Input;
    using MobileApp.Services;
    using Xamarin.Forms;

    namespace MobileApp.ViewModel
    {
    public class ApprovalMainModel : INotifyPropertyChanged
    {
    Common objCom = new Common();

        public event PropertyChangedEventHandler PropertyChanged;
    
        public List<ApprovalData> listApprovalData { get; set; }
        public ApprovalMainModel(List<ApprovalData> list)
        {
            listApprovalData = list;
            allapprovalData = new ObservableCollection<SelectableItemWrapper<ApprovalData>>(listApprovalData
                .Select(pk => new SelectableItemWrapper<ApprovalData> { Item = pk }));
        }
        public async void bindData(string stripAddress, string url)
        {
            var jsonString = await objCom.GettingJSONFromWebService(stripAddress, url);
            if (!string.IsNullOrWhiteSpace(jsonString))
            {
                if (jsonString != "ConnectFailure")
                {
                    listApprovalData = JsonConvert.DeserializeObject<List<ApprovalData>>(jsonString);
                }
            }
        }
    
        private ObservableCollection<SelectableItemWrapper<ApprovalData>> _apprvalitems;
        public ObservableCollection<SelectableItemWrapper<ApprovalData>> allapprovalData
        {
            get { return _apprvalitems; }
            set { _apprvalitems = value; RaisePropertyChanged(); }
        }
    
        private ObservableCollection<ApprovalData> _selectedleaveItems;
        public ObservableCollection<ApprovalData> SelectedPokemons
        {
            get
            {
                return _selectedleaveItems ?? new ObservableCollection<ApprovalData>();
            }
            private set
            {
                _selectedleaveItems = value; RaisePropertyChanged();
            }
        }
    
        #region Commands
    
        private ICommand _getSelectedItemsCommand;
        public ICommand GetSelectedItemsCommand
        {
            get
            {
                return _getSelectedItemsCommand ??
                    (_getSelectedItemsCommand = new Command(
                        () =>
                        {
                            SelectedPokemons = GetSelectedPokemons();
                            // Navigation is not 
                            //await App.Current.MainPage.Navigation.PushAsync(new SelectedPokemonPage());
                        }));
            }
        }
    
        #endregion
    
        ObservableCollection<ApprovalData> GetSelectedPokemons()
        {
            var selected = allapprovalData
                .Where(p => p.IsSelected)
                .Select(p => p.Item)
                .ToList();
            return new ObservableCollection<ApprovalData>(selected);
        }
    
        void SelectAll(bool select)
        {
            foreach (var p in allapprovalData)
            {
                p.IsSelected = select;
            }
        }
    
        void RaisePropertyChanged([CallerMemberName] string propertyName = null)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
    

    }

    Binding Above model into below ,

    public LeaveApprovalList(List listad, string status)
    {
    BindingContext = new ApprovalMainModel(listad);
    PokemonsViewModel = new ApprovalMainModel(listad);
    listadNew = listad;
    list = new ListView { RowHeight = 80 };
    list.SetBinding(ListView.ItemsSourceProperty, "allapprovalData");
    var template = new DataTemplate(typeof(PokemonSelectableCell));
    template.SetBinding(PokemonSelectableCell.NameProperty, "Item.EmpFirstName");
    template.SetBinding(PokemonSelectableCell.Duration, "Item.Duration");
    template.SetBinding(PokemonSelectableCell.leaveType, "Item.leaveType");
    list.ItemTemplate = template;
    //var getSelectedItemsButton = new ToolbarItem()
    //{
    // Text = "Elegir"
    //};
    //getSelectedItemsButton.SetBinding(ToolbarItem.CommandProperty, "GetSelectedItemsCommand");
    //ToolbarItems.Add(getSelectedItemsButton);

            StackLayout sl = new StackLayout();
            sl.Children.Add(list);
            var btn = new Button { Text = "Save" };
            btn.SetBinding(Button.CommandProperty, "GetSelectedItemsCommand");
            btn.Clicked += btnSave_Clicked;
            sl.Children.Add(btn);
            Content = sl;
        }
    

    private void btnSave_Clicked(object sender, EventArgs e)
    {
    ObservableCollection oad = new ObservableCollection();
    oad = PokemonsViewModel.SelectedPokemons;
    for (int i = 0; i < oad.Count; i++)
    {
    int j = 0;
    j = Convert.ToInt16(oad[1].leaveId);
    }

        }
    

    if i access like this, the count will be showing always 0 in btnsave_clicked.

  • Mani787Mani787 INMember ✭✭✭

    Hello sir, Here i have attached sample application. i dont know to get the selected item of listview control on same page while clicking the button control.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Sorry @Mani787 but I don't understand your problem
    I have downloaded your Project and run on Emulator

    When you change "switch" status from "false" to "true" and press "get" button, the "GetSelectedItemsCommand" fires and you have, in "SelectedPockemons", the list with "switch" set to "true".

  • Mani787Mani787 INMember ✭✭✭

    Yes Alessandro. But i want to sent the selected listview item to database if i press save button. In my sample i have used one button control and binded inside into stack layout. did you see that. while clicking that button, i want to pass only selected listview item iD to database how to do that? pls tell me.

    For example i want to show the selected liestview item ID value in alert message means, How to do that? pls tell me.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Sorry, really I don't understand.

    SelectedPockemons containts all your Selected items. You can do what you want with these data.

  • Mani787Mani787 INMember ✭✭✭

    how to bind SelectedPockemons items into another list item on button click.

  • Mani787Mani787 INMember ✭✭✭

    yes, SelectedPockemons containts all the Selected items. but i want to bind this selectedPockemons items into new list item how to do that on same page.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    you are alreading binding it in SelectedPokemonPage

  • Mani787Mani787 INMember ✭✭✭

    not listview control.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Your ListView in PockemonListPage is in Binding with Pokemons property.

    You have to set Pokemons property with SelectedPockemons data to see these data in PockemonListPage's Listview

  • Mani787Mani787 INMember ✭✭✭

    While clicking an button in PockemonListPage, i want to show an total selected item count in alert message. how to get the selected item count value in button click. i.e btnsave_clicked in PockemonListPage.

  • Mani787Mani787 INMember ✭✭✭

    i have one more doubt here... how to bind SelectedPokemons items into generic list.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    bind into generic list? what is?

Sign In or Register to comment.