Search code examples
c#wcfwindows-phone-8longlistselector

Longlist is not creating properly


I want to populate longlist via WCF service. The compilation goes without errors, but the result of the executed program looks like three lines of this: PhoneApp1.ServiceReference1.worker instead of the name and other data I want to display. My service implementation is:

public IEnumerable<worker> GetStuffList()
    {
        List<worker> stuffList = new List<worker>();
        stuffList.Add(new worker("John", 23, true));
        stuffList.Add(new worker("Nick", 22, true));
        stuffList.Add(new worker("Gill", 23, false)); 
        return stuffList;
    }

    private List<Group<worker>> GetStuffEnumerable()
    {
        IEnumerable<worker> stuffList = GetStuffList();
        return GetItemGroups(stuffList, c => c.Age.ToString());
    }

    private static List<Group<T>> GetItemGroups<T>(IEnumerable<T> itemList, Func<T, string> getKeyFunc)
    {
        IEnumerable<Group<T>> groupList = from item in itemList
                                          group item by getKeyFunc(item) into g
                                          orderby g.Key
                                          select new Group<T>(g.Key, g);

        return groupList.ToList();
    }

    public class Group<T> : List<T>
    {
        public Group(string name, IEnumerable<T> items)
            : base(items)
        {
            this.Title = name;
        }

        public string Title
        {
            get;
            set;
        }
    }

Solution

  • You need to speficy an ItemTemplate for the LongListSelector. Without it, it is showing object.ToString().

    Something like this:

    <LonglistSelector ...>
        <LongListSelector.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Name}"/>
                </StackPanel>
            </DataTemplate>        
        </LongListSelector.ItemTemplate>
    </LongListSelector/>