Carson Vo
Carson Vo

Reputation: 546

Windows phone 8: get value item of listbox

I have a listbox. Every item has an image and title (binding from my list). When I click an item of the listbox, how do I get the title value of that item.

Upvotes: 1

Views: 3974

Answers (2)

Romasz
Romasz

Reputation: 29792

There are already few similar questions (first, second). I'll try to show you an example (little extending this what @KirtiSagar (if it helps accept his solution as it's the same method) has said):

Let's assume that your ItemClass look like this:

public class ItemClass
{
    public Uri ImagePath { get; set; }
    public string Tile { get; set; }  // I used string as an example - it can be any class
}

And you Bind it to your ListBox like this:

<ListBox Name="myList" Grid.Row="2">
   <ListBox.ItemTemplate>
      <DataTemplate>
          <StackPanel Orientation="Horizontal">
              <Image Source="{Binding ImagePath}"/>
              <TextBlock Text="{Binding Tile}"/>
          </StackPanel>
      </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>

It is a very simple example but should show you an overview how it works.

Then in my Page and Constructor I need to add my Collection and subscribe to events:

ObservableCollection<ItemClass> items = new ObservableCollection<ItemClass>();

public MainPage()
{
    InitializeComponent();

    myList.ItemsSource = items;
    myList.SelectionChanged += myList_SelectionChanged;
}

The SelectinChanged event can be used for your purpose in many ways. For example you can use its SelectionChangedEventArgs properties. I will show some methods which will produce the same result. I mixed some things on purpose just to show how it can be done.

private void myList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   if (myList.SelectedItem != null)
   {
       string myTile = ((sender as ListBox).SelectedItem as ItemClass).Tile;
       // you can also get your item like this - using EventArgs properties:
       string myTileToo = ((ItemClass)(e.AddedItems[0])).Tile;
   }
   // also you can get this by SelectedIndex and your Item Collection
   if ((sender as ListBox).SelectedIndex >= 0)
   {
       string myTileThree = items[myList.SelectedIndex].Tile;
   }
}

Note that your LisBox can work in different SelectionModes for example Multipe - you can also try to use that if you need it (there are properties SelectedItems and AddedItems / RemovedItems, which are IList).

Upvotes: 2

KirtiSagar
KirtiSagar

Reputation: 584

create an event in ListBox called "SelectionChanged" and map it the method in the code behind of the XAML file. In the .cs file get the value from myListBox.SelectedItem and cast it to your list item type.

EX: in XAML file

<ListBox x:Name="myListBox" ItemsSource="{Binding Items}"     
  SelectionChanged="myListBox_SelectionChanged">

in xaml.cs file:

private void myListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var mySelectedItem = myListBox.SelectedItem as myObject;
}

I hope this helps.

Upvotes: 7

Related Questions