LongListSelector C# - How to get SelectedItem as string from grouped data list - Windows Phone Silverlight 8.1

124 Views Asked by At

I'm building my first WP Silverlight 8.1 app in C# so still quite novice!

I've got a LongListSelector in my app which allows a user to choose a room. When a user taps on a room I want the room name (eg 'N01', 'N07', 'N12' - see code below!) to be stored as a string.

I have the rooms stored in a grouped list - the list 'dataSource' is bound to the LongListSelector (which btw is called 'longListSelectorState').

Here is my C# code of the list:

namespace WPLongListSelectorDemo
{
public partial class MainPage : PhoneApplicationPage
{
    List<RoomList> dataSource;
    // Constructor
    public MainPage()
    {

        InitializeList();
        InitializeComponent();
   List<RoomGroup<RoomList>> DataSource = RoomGroup<RoomList>.CreateGroups(dataSource,
            System.Threading.Thread.CurrentThread.CurrentUICulture,
            (RoomList s) => { return s.RoomName; }, true);
        longListSelectorState.ItemsSource = DataSource;
        // Sample code to localize the ApplicationBar
        //BuildLocalizedApplicationBar();
    }

    private void InitializeList()
    {
        dataSource = new List<RoomList>();
        //North Rooms - Classrooms
        dataSource.Add(new RoomList("N01", "North"));
        dataSource.Add(new RoomList("N02", "North"));
        dataSource.Add(new RoomList("N03", "North"));
        dataSource.Add(new RoomList("N04", "North"));
        dataSource.Add(new RoomList("N05", "North"));
        dataSource.Add(new RoomList("N06", "North"));
        dataSource.Add(new RoomList("N07", "North"));
        dataSource.Add(new RoomList("N08", "North"));
        dataSource.Add(new RoomList("N09", "North"));
        dataSource.Add(new RoomList("N10", "North"));
        dataSource.Add(new RoomList("N11", "North"));
        dataSource.Add(new RoomList("N12", "North"));
        dataSource.Add(new RoomList("N13", "North"));
        dataSource.Add(new RoomList("N14", "North"));
        dataSource.Add(new RoomList("N15", "North"));
        dataSource.Add(new RoomList("N16", "North"));
        dataSource.Add(new RoomList("N17", "North"));
        dataSource.Add(new RoomList("N18", "North"));
        dataSource.Add(new RoomList("N19", "North"));
        dataSource.Add(new RoomList("N20", "North"));
        dataSource.Add(new RoomList("N21", "North")); 

The list is actually longer than that but I've cut it down a bit here. There's over 100 elements in the list.

I've tried using the following code to get the SelectedItem from the list and then display it in a message box.

  private void longListSelectorState_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var selectedRoom = longListSelectorState.SelectedItem;
        MessageBox.Show(selectedRoom.ToString());

    }

But the message box just shows 'WPLongListSelectorDemo.RoomList' as the SelectedItem. WPLongListSelector is the namespace and RoonList is the list name, so that makes sense I guess, but it's not what I want! I want it to say 'N01', for example.

How do I go about getting the actual room from the list as the SelectedItem as a string?

Thanks!

1

There are 1 best solutions below

0
bazsibazsi On

I don't know what a grouped list exactly is, but if your RoomList class looks for example somehow like this:

internal class RoomList
{
    public string Location { get; set; }
    public string ClassNumber { get; set; }
}

and you create a new instance of RoomList

RoomList selectedRoom  = new RoomList()
{
    Location = "north",
    ClassNumber = "n01"
}

and you call selectedRoom .ToString() you will get 'WPLongListSelectorDemo.RoomList' because you converts the type itself to string (which returns the namespace and the name of the type), not the string property of the instance selectedRoom.

Instead, you should do this:

MessageBox.Show(selectedRoom .Location.ToString()) 

or

MessageBox.Show(selectedRoom.ClassNumber.ToString()).

(In this case, you don't even need ToString(), because both Location and ClassNumber are strings.)

You can also override the ToString() method of RoomList:

internal class RoomList
{
    public string Location { get; set; }
    public string ClassNumber { get; set; }

    public override string ToString()
    {
        return Location + ", " + ClassNumber;
    } 
}

In that case, selectedRoom.ToString() will returns 'north, n01'.