在ListView Windows Phone中获取项目 [英] Get Items in ListView Windows Phone

查看:80
本文介绍了在ListView Windows Phone中获取项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在列表视图中选择项目.每次我在列表视图中选择一个项目时,它总是会得到最后一个项目.这是我的代码:

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

隐藏代码

感谢您的帮助,谢谢.

已更新:

人员班

   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

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);


    }

推荐答案

正如@mindOfAi所说,您应该能够使用ItemClick事件.它发生在列表视图中的项收到交互并且IsItemClickEnabled时属性是真实的.我们可以通过 ItemClickEventArgs.ClickedItem 属性.

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.

在发布的代码中,似乎您定义了人员实例,并通过buttonAdd_Click事件设置了数据.我们找不到在哪里设置 ListViewItemsource .

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.

您应该能够将您的person对象添加到集合中,并且我们可以将集合设置为Itemsource.然后我们可以通过ItemClick事件获得ClickedItem.

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.

例如:

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;
}

这篇关于在ListView Windows Phone中获取项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆