0

I want to bind list of objects to toolkit LongListSelector Windows Phone 7 as what i do in Windows Phone 8. Anyone could tell my where's wrong?

Nothing show in the LongListSelector

XAML

<toolkit:LongListSelector Name="MyLLS">
    <toolkit:LongListSelector.ItemTemplate>
        <DataTemplate>
            <Grid Background="‪#‎4A6EA9‬" Height="60" Margin="2">
                <TextBlock Text="{Binding Title}"/>
            </Grid>
        </DataTemplate>
    </toolkit:LongListSelector.ItemTemplate>
</toolkit:LongListSelector>

Class

public class NewsItem
{
    public string Link { get; set; }
    public string Title { get; set; }
}


MainPage()
{
    // something already
    MyLLS.ItemsSource = new List<NewsItem> { 
        new NewsItem { Link = "http://google.com", Title= "Google Search" }
    };
}
ekad
  • 14,436
  • 26
  • 44
  • 46
HelloWindowsPhone
  • 680
  • 10
  • 19

2 Answers2

1

I've not played much with WP7 ToolKit but I've managed to display list like this:

In XAML - removed Grid and added isFlatList:

<toolkit:LongListSelector Name="MyLLS" IsFlatList="True">
    <toolkit:LongListSelector.ItemTemplate>
        <DataTemplate>
              <TextBlock Text="{Binding Title}"/>
        </DataTemplate>
    </toolkit:LongListSelector.ItemTemplate>
</toolkit:LongListSelector>

In code behind:

ObservableCollection<NewsItem> myList = new ObservableCollection<NewsItem>();

MainPage()
{
    InitializeComponent();
    // something already
    MyLLS.ItemsSource = myList;
    myList.Add(new NewsItem { Link = "http://google.com", Title= "Google Search" });
}

I haven't got much time to play with the code above, but maybe it will help. I would advise to read some tutorials - WP7 LongListSelector in depth Part1, WP7 LongListSelector in depth Part2 and probably more.

Romasz
  • 29,662
  • 13
  • 79
  • 154
  • Not works. I know how to bind data on Windows Phone 8, but there is a different on Windows Phone 7 (toolkit:LongListSelector) can not do the same – HelloWindowsPhone Feb 10 '14 at 10:04
  • @iexplore I've edited my code and as I've tested it at least display List. As I've written - beacause of the lack of time - it is like this, but maybe will help a little. – Romasz Feb 10 '14 at 10:55
  • Just add IsFlatList="True" and problem solved. Not need ObservableCollection instead List. Thanks for your useful help – HelloWindowsPhone Feb 10 '14 at 11:07
  • @iexplore List or ObservableCollection depends on your code, and you are right that in simple cases List can be sufficient. – Romasz Feb 10 '14 at 11:26
0

I think you should do InitializeComponent(); first in MainPage constrator.

MainPage()
{
    // Init method must work
    InitializeComponent(); 
    // something already
    MyLLS.ItemsSource = new ObservableCollection<NewsItem> { 
        new NewsItem { Link = "http://google.com", Title= "Google Search" }
    };
}
Chris Shao
  • 8,231
  • 3
  • 39
  • 37