Justei
Topic Author
Posts: 17
Joined: 28 Aug 2013, 21:03

Get a ListBoxItem when clicked.

03 Sep 2013, 13:47

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 :)
 
Shorinji
Posts: 24
Joined: 16 Aug 2013, 19:45
Location: Shanghai

Re: Get a ListBoxItem when clicked.

03 Sep 2013, 15:49

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>();
}
 
Justei
Topic Author
Posts: 17
Joined: 28 Aug 2013, 21:03

Re: Get a ListBoxItem when clicked.

03 Sep 2013, 16:02

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 :).
 
User avatar
sfernandez
Site Admin
Posts: 3197
Joined: 22 Dec 2011, 19:20

Re: Get a ListBoxItem when clicked.

04 Sep 2013, 01:15

Thanks for all the information you are providing. It may be very useful for other users :D
 
jc_lvngstn
Posts: 34
Joined: 23 Sep 2013, 03:03

Re: Get a ListBoxItem when clicked.

07 Oct 2013, 21:53

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.
 
User avatar
sfernandez
Site Admin
Posts: 3197
Joined: 22 Dec 2011, 19:20

Re: Get a ListBoxItem when clicked.

07 Oct 2013, 22:48

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.

Who is online

Users browsing this forum: No registered users and 11 guests