Listview SelectedItem

ElzuElzu INMember ✭✭

I have a list view with one label and one image. When I select one row I want to send my label text of that selected row to view model.How can I do this?

Tagged:

Best Answer

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    Use SelectedItem property
  • ElzuElzu INMember ✭✭

    yes,I tried that.But I get null..

    this is my listview
    <ListView x:Name="LstCountryCode"
    HasUnevenRows="True"
    SelectedItem="{Binding CountryCodeNumBindSelected}"

                      ItemSelected="LstCountryCode_ItemSelected"
                      RowHeight="50">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="*"/>
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="*"/>
                                    </Grid.RowDefinitions>
                                    <Image x:Name="imgCountryFlag"
                                           Source="{Binding CountryFlag}"
                                           Grid.Column="0"
                                           Grid.Row="0"/>
                                    <Label x:Name="lblCountryCode"
                                           Text="{Binding CountryCodeNum}"
                                           Grid.Column="1"
                                           Grid.Row="0"/>
                                </Grid>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
    
            </ListView> 
    

    this is my cs page

        public partial class CountryPicker : PopupPage
    {
        List<CountryCode> lstCountryCode;
    
    
        public CountryPicker()
        {
    
            InitializeComponent();
    
            lstCountryCode = new List<CountryCode>()
            {
                new CountryCode
                {
                    CountryCodeNum="+91",
                    CountryFlag=ImageSource.FromResource("GLYDE_MOBAPP.Images.DocFail.png")
                },
                 new CountryCode
                {
                    CountryCodeNum="+92",
                   CountryFlag=ImageSource.FromResource("GLYDE_MOBAPP.Images.DocSuccess.png")
                },
                   new CountryCode
                {
                   CountryCodeNum="+94",
                   CountryFlag=ImageSource.FromResource("GLYDE_MOBAPP.Images.fare-esti.png")
                },
    
            };
            LstCountryCode.ItemsSource = lstCountryCode;
            var vm = App.Locator.RiderSignUps;
    
            BindingContext = vm;
    
        }
    

    }

    public class CountryCode
    {
    public ImageSource CountryFlag { get; set; }
    public string CountryCodeNum { get; set; }
    }

    and the viewmodel is

    public List Countrycode;
    public int countryCodeNumBindSelected;
    private CountryCode SelectedCountryCode;
    public int CountryCodeNumBindSelected
    {
    get
    {
    return countryCodeNumBindSelected;
    }
    set
    {
    if (countryCodeNumBindSelected != value)
    {
    countryCodeNumBindSelected = value;

                    // trigger some action to take such as updating other labels or fields
                    OnPropertyChanged(nameof(countryCodeNumBindSelected));
                    SelectedCountryCode = Countrycode[countryCodeNumBindSelected];
                }
            }
        }
    

    string SelectedCountry = Countrycode[CountryCodeNumBindSelected].CountryCodeNum

  • ElzuElzu INMember ✭✭

    Thanks for your help Charwaka....It worked. :)

Sign In or Register to comment.