在ListView Windows Phone中获取项目

时间:2017-02-27 03:18:37

标签: listview uwp

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

XAML

enter image description here

代码背后

enter image description here

感谢您的帮助,谢谢。

更新:

人类

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


    }

2 个答案:

答案 0 :(得分:0)

我不推荐这个。不要使用ListView的Tapped事件进行项目选择。为什么不只使用ItemClick事件?

首先,在ListView上启用IsItemClickEnabled XAML。将其设置为false为true。然后添加ItemClick事件。

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

希望它有所帮助!

答案 1 :(得分:0)

正如@mindOfAi所说,你应该能够使用ItemClick事件。当列表视图中的项目收到交互,并且IsItemClickEnabled属性为true时,就会发生这种情况。我们可以通过ItemClickEventArgs.ClickedItem属性获取项目。

从您发布的代码中,您似乎定义了一个人员实例,并通过buttonAdd_Click事件设置数据。我们无法找到您在ListView设置Itemsource的位置。

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

例如:

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