Home > Enterprise >  How can I select an item in a ListView in Xamarin.Forms with XAML
How can I select an item in a ListView in Xamarin.Forms with XAML

Time:07-08

I am currently trying to select an item in a ListView in Xamarin.Forms with XAML. When I push the Add-button the label gets the right value, the new value is added to the ListView but I can't manage it to select the new value in the ListView. Anyone an idea why?

My MainPage.xaml:

<?xml version="1.0" encoding="utf-8" ?>

<ContentPage Title="Test" xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:BarcodeScanning.ViewModels" x:Class="BarcodeScanning.MainPage"
    Visual="Material">
    <ContentPage.BindingContext>
        <local:ScanningViewModel x:Name="viewModel"/>
    </ContentPage.BindingContext>
    <StackLayout Margin="0" Orientation="Vertical">
        <StackLayout x:Name="slScannedItem" Orientation="Horizontal" Spacing="0" Margin="0,0,0,0" Padding="0,0,0,0">
            <Label Text="{Binding number}" VerticalOptions="Center" Margin="10,0,0,0"></Label>
            <Button x:Name="btnAdd" Text="Add" VerticalOptions="Center" Command="{Binding CmdAddClick}" CommandParameter="1" IsVisible="True"/>
            <ListView ItemsSource="{Binding numbers}" SelectedItem="{Binding selecteditem}">
                
            </ListView>
        </StackLayout>
    </StackLayout>
</ContentPage>

My ScanningViewModel:

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;

using Xamarin.Forms;

namespace BarcodeScanning.ViewModels
{
    public class ScanningViewModel : INotifyPropertyChanged
    {
        private string _number { get; set; }
        public string number
        {
            get => _number;
            set
            {
                _number = value;
                OnPropertyChanged();
            }
        }

        private ObservableCollection<string> _numbers { get; set; }
        public ObservableCollection<string> numbers
        {
            get => _numbers;
            set
            {
                _numbers = value;
                OnPropertyChanged();
            }
        }

        private string _selecteditem { get; set; }
        public string selecteditem
        {
            get => _number;
            set
            {
                _selecteditem = value;
                OnPropertyChanged();
            }
        }

        private Command addclickcommand;

        public ScanningViewModel()
        {
            number = "5";
            numbers = new ObservableCollection<string>();
            addclickcommand = new Command<string>(btnLoadPicking_Clicked);
        }

        public Command CmdAddClick
        {
            get { return addclickcommand; }
        }

        

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        

        private void btnLoadPicking_Clicked(string value)
        {
            number = (int.Parse(number)   int.Parse(value)).ToString();
            numbers.Add(number);
            selecteditem = number;
        }

        
    }
}

CodePudding user response:

I think the issue is your selected item property which should be like this:

private string _selecteditem { get; set; }
    public string selecteditem
    {
        get => _selecteditem;
        set
        {
            _selecteditem = value;
            OnPropertyChanged();
        }
    }

CodePudding user response:

Like FreakyAli said, you need to change the selecteditem property first. And then change the Binding Modes on SelectedItem property of ListView.

Xaml:

  <ListView ItemsSource="{Binding numbers}" SelectedItem="{Binding selecteditem, Mode=TwoWay}" >

        </ListView>
   

enter image description here

  • Related