Get data from clicked item in ListBox

血红的双手。 提交于 2019-12-02 06:04:07

There are few ways to do it:

private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs args)
{
    if (listBox1.SelectedIndex == -1) return;

    Attractions first = listbox1.SelectedItem as Attractions ;
    Attractions second = (Attractions)listBox1.Items[listBox1.SelectedIndex];
    Attractions third = (sender as ListBox).SelectedItem as Attractions;
    Attractions fourth = args.AddedItems[0] as Attractions;

    Debug.WriteLine(" You selected " + first.AttractionName);
}

With SelectedItem (Index) you get an item that is Type of your ItemsSource Collection. Once you get the item you can do what you want with it.

Inside SelectionChange Event put the following code

Attractions  lbi = (sender as ListBox).SelectedItem as Attractions;

you can access the properties of the class using

lbi.price

write this code

This Can Help You

void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs args)
    {
        Debug.WriteLine(" You selected " +listBox1.Items[listBox1.SelectedIndex].ToString ());
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!