Page 1 of 1

Get a ListBoxItem when clicked.

Posted: 03 Sep 2013, 13:47
by Justei
Hi there, I'm messing around with ListBoxes and am trying to figure out the best way of getting the content of a ListBoxItem when I click it.
Here's what I have currently:

public void AddToList()
{
 ...
 _characterList.GetItems().Add(testItem);
 testItem.Selected += this.OnSelectedClick;
}

void OnSelectedClick(BaseComponent sender, RoutedEventArgs args)
{
 // This is where I can't figure out how to get the clicked ListBoxItem that has been clicked.
}

Hopefully you guys understand what I'm asking :)

Re: Get a ListBoxItem when clicked.

Posted: 03 Sep 2013, 15:49
by Shorinji
Hi,
I'm new to this too but i think it should be this :
void OnSelectedClick(BaseComponent sender, RoutedEventArgs args)
{
    ListBoxItem listBoxItem = sender.As<ListBoxItem>();
}

Re: Get a ListBoxItem when clicked.

Posted: 03 Sep 2013, 16:02
by Justei
Yes! That worked like a charm, thanks! :)
Still figuring this system out a bit, hopefully this thread is helpful to some other dev later on as well :).

Edit:
I wanted to post another little snippet that might help others in need. An alternative way of getting the current selected Item is:
ListBoxItem _selectedItem = _YourList.GetSelectedItem().As<ListBoxItem>();
Just as a tip :).

Re: Get a ListBoxItem when clicked.

Posted: 04 Sep 2013, 01:15
by sfernandez
Thanks for all the information you are providing. It may be very useful for other users :D

Re: Get a ListBoxItem when clicked.

Posted: 07 Oct 2013, 21:53
by jc_lvngstn
Is there a way to get the currently selected items from a listbox, such as

ListBox.SelectedItems

?

It's documented, but doesn't seem to exist.

Re: Get a ListBoxItem when clicked.

Posted: 07 Oct 2013, 22:48
by sfernandez
Is there a way to get the currently selected items from a listbox, such as

ListBox.SelectedItems

?

It's documented, but doesn't seem to exist.
You are right, that function was not exposed to the Unity API. I've just added so it will be available in the following release due this week.