Get Items in ListView Windows Phone

亡梦爱人 提交于 2019-12-25 09:15:34

问题


I wanted to get items selected in my listview. Every time I select item in my list view it always gets the last item. Here's my code:

XAML

Code Behind

Appreciate your help, thank you.

Updated:

Person Class

   private string name;
        private string imagePath;
        private string contact;
        private string gender;

        public Person()
        {

        }

        public String Name
        {
            get { return name; }
            set
            {
                name = value;
            }

        }

        public String Gender
        {
            get { return gender; }
            set
            {
                gender = value;
            }

        }

        public String ImagePath
        {
            get { return imagePath; }
            set
            {
                imagePath = value;
            }
        }

        public String Contact
        {
            get { return contact; }
            set
            {
                contact = value;
            }
        }

MainPage.cs

 Person person = new Person();
    public MainPage()
    {
        this.InitializeComponent();

        this.NavigationCacheMode = NavigationCacheMode.Required;
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {

    }

    private void buttonAdd_Click(object sender, RoutedEventArgs e)
    {
        person.Name = textBoxName.Text.ToString();
        person.Contact = textBoxContact.Text.ToString();
        listViewDirectory.Items.Add(person);

        textBoxName.Text = "";
        textBoxContact.Text = "";
        radioButtonMale.IsChecked = false;
        radioButtonFemale.IsChecked = false;
        imageGender.Source = null;
    }

    private void radioButtonIsClicked(object sender, RoutedEventArgs e)
    {
        var selectedRadio = myStackPanel.Children.OfType<RadioButton>().FirstOrDefault(r => r.IsChecked == true);

        if (selectedRadio.Content.ToString().Equals("Male"))
        {
            imageGender.Source = new BitmapImage(new Uri(
       "ms-appx:///Assets/tiles/boy.png", UriKind.Absolute));
            person.ImagePath = "ms-appx:///Assets/tiles/boy.png";
            person.Gender = selectedRadio.Tag.ToString();
        }
        else if (selectedRadio.Content.ToString().Equals("Female"))
        {
            imageGender.Source = new BitmapImage(new Uri(
       "ms-appx:///Assets/tiles/girl.png", UriKind.Absolute));
            person.ImagePath = "ms-appx:///Assets/tiles/girl.png";
            person.Gender = selectedRadio.Tag.ToString();
        }
    }

    private void listViewDirectory_ItemClick(object sender, ItemClickEventArgs e)
    {
        Person obj = (Person)e.ClickedItem;

        Debug.WriteLine(obj.Name);


    }

回答1:


As @mindOfAi said, you should be able to use the ItemClick event.It occurs when an item in the list view receives an interaction, and the IsItemClickEnabled property is true. And we can get item by the ItemClickEventArgs.ClickedItem property.

From the code that you posted, it seems you define a person instance, and set the data by the buttonAdd_Click event. We can not find where do you set the Itemsource of the ListView.

You should be able to add your person object to the collection, and we can set the collection to the Itemsource. Then we can get the ClickedItem by the ItemClick event.

For example:

private ObservableCollection<Person> Persons;

public MainPage()
{
    this.InitializeComponent();
    this.NavigationCacheMode = NavigationCacheMode.Required;
    Persons = new ObservableCollection<Person>();

    for (int i = 0; i < 10; i++)
    {
        Persons.Add(new Person());
        Persons[i].Contact = "Contact" + i;
        Persons[i].Gender = "Gender" + i;
        Persons[i].ImagePath = "ms - appx:///Assets/tiles/boy.png";
        Persons[i].Name = "Name" + i;
    }
    listViewDirectory.ItemsSource = Persons;
}



回答2:


I wouldn't recommend this. Don't use ListView's Tapped event for item selection. Why not use just ItemClick event?

First, enable your ListView's IsItemClickEnabled on your XAML. Set it from false to true. Then add the ItemClick event.

private void ListView_ItemClick(object sender, ItemClickEventArgs e)
{
   //Use e.ClickedItem to get the data;
}

Hope it helps!



来源:https://stackoverflow.com/questions/42477285/get-items-in-listview-windows-phone

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!