locked
How to access a control inside the listview RRS feed

  • Question

  • User318961 posted

    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?

    Monday, July 31, 2017 3:29 PM

Answers

  • User2148 posted

    Why do you have to use btnsave_clicked?

    You are using ViewModel

    You should use GetSelectedItemsCommand

    Something like

        public ICommand GetSelectedItemsCommand
        {
            get
            {
                return _getSelectedItemsCommand ??
                    (_getSelectedItemsCommand = new Command(
                        async () =>
                        {
                            SelectedPokemons = GetSelectedPokemons();
    
                            await Application.Current.MainPage.DisplayAlert("Count", SelectedPokemons.Count().ToString(), "Ok");
                            // Navigation is not 
                            //await App.Current.MainPage.Navigation.PushAsync(new SelectedPokemonPage());
                        }));
            }
        }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, August 2, 2017 10:09 AM

All replies

  • User2148 posted

    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.

    Monday, July 31, 2017 3:43 PM
  • User324047 posted

    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/

    Tuesday, August 1, 2017 4:28 AM
  • User318961 posted

    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.

    Tuesday, August 1, 2017 2:31 PM
  • User318961 posted

    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.

    Tuesday, August 1, 2017 3:23 PM
  • User2148 posted

    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?

    Tuesday, August 1, 2017 3:41 PM
  • User318961 posted

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

    Tuesday, August 1, 2017 4:11 PM
  • User2148 posted

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

    Tuesday, August 1, 2017 4:13 PM
  • User318961 posted

    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.

    Tuesday, August 1, 2017 4:23 PM
  • User318961 posted

    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.

    Wednesday, August 2, 2017 6:57 AM
  • User2148 posted

    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".

    Wednesday, August 2, 2017 8:17 AM
  • User318961 posted

    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.

    Wednesday, August 2, 2017 8:46 AM
  • User2148 posted

    Sorry, really I don't understand.

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

    Wednesday, August 2, 2017 9:03 AM
  • User318961 posted

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

    Wednesday, August 2, 2017 9:12 AM
  • User318961 posted

    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.

    Wednesday, August 2, 2017 9:15 AM
  • User2148 posted

    you are alreading binding it in SelectedPokemonPage

    Wednesday, August 2, 2017 9:15 AM
  • User318961 posted

    not listview control.

    Wednesday, August 2, 2017 9:21 AM
  • User2148 posted

    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

    Wednesday, August 2, 2017 9:22 AM
  • User318961 posted

    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.

    Wednesday, August 2, 2017 9:50 AM
  • User2148 posted

    Why do you have to use btnsave_clicked?

    You are using ViewModel

    You should use GetSelectedItemsCommand

    Something like

        public ICommand GetSelectedItemsCommand
        {
            get
            {
                return _getSelectedItemsCommand ??
                    (_getSelectedItemsCommand = new Command(
                        async () =>
                        {
                            SelectedPokemons = GetSelectedPokemons();
    
                            await Application.Current.MainPage.DisplayAlert("Count", SelectedPokemons.Count().ToString(), "Ok");
                            // Navigation is not 
                            //await App.Current.MainPage.Navigation.PushAsync(new SelectedPokemonPage());
                        }));
            }
        }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, August 2, 2017 10:09 AM
  • User318961 posted

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

    Wednesday, August 2, 2017 10:24 AM
  • User2148 posted

    bind into generic list? what is?

    Wednesday, August 2, 2017 11:54 AM