1. Shan Ali Khan's Avatar
    Hi ,

    Im unable to bind long list selector in my windows phone 8 application.

    FOllowing is the code, im beginner .please have a look on the following code
    [CODE] public partial class MainPage : PhoneApplicationPage
    {
    List<Newspaper> news = new List<Newspaper>();


    public class ViewModel {
    public ObservableCollection<Newspaper> news = new ObservableCollection<Newspaper>();

    public ViewModel() {
    news.Add(new Newspaper() { Name = "Tribune" });
    }


    }



    // Constructor
    public MainPage()
    {
    InitializeComponent();
    //var tmp = new Newspaper();
    //tmp.Name = "tribune";
    //news.Add(tmp);

    //tmp = new Newspaper();
    //tmp.Name = "tribune";
    //news.Add(tmp);
    //tmp = new Newspaper();
    //tmp.Name = "The Nation";
    //news.Add(tmp);

    lstNewspapers.DataContext = new ViewModel().news;

    // Sample code to localize the ApplicationBar
    //BuildLocalizedApplicationBar();
    }[/CODE]

    [CODE]public class Newspaper :INotifyPropertyChanged

    {
    private string name;

    public string Name
    {
    get { return name; }
    set { name = value;
    OnPropoertyChanged("Name");
    }
    }

    public void OnPropoertyChanged(string propName) {
    if (this.PropertyChanged!=null)
    {
    this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    }[/CODE]

    Markup :
    phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Key="itemNewsPaper">
    <StackPanel Orientation="Horizontal">

    <TextBlock Text="{Binding Name, Mode=OneWay}" Width="200px"></TextBlock>

    </StackPanel>
    </DataTemplate>

    </phone:PhoneApplicationPage.Resources>

    <phone:LongListSelector x:Name="lstNewspapers" LayoutMode="List" IsGroupingEnabled="False"
    ItemTemplate="{StaticResource itemNewsPaper}">

    </phone:LongListSelector>
    11-02-2013 01:05 PM
  2. HG Software's Avatar
    As far as I can see, you don't set the ItemsSource of your LongListSelector. Either replace
    Code:
    lstNewspapers.DataContext = new ViewModel().news;
    with
    Code:
    lstNewspapers.ItemsSource = new ViewModel().news;
    or add a binding in XAML, e.g.
    Code:
    <phone:LongListSelector x:Name="lstNewspapers" LayoutMode="List" IsGroupingEnabled="False" ItemTemplate="{StaticResource itemNewsPaper}" ItemsSource="{Binding Path=.}">
    Hope this helps, didn't test it. I only used the LongListSelector with grouping so far and then it is much more complicated.
    11-03-2013 08:21 AM
  3. Shan Ali Khan's Avatar
    I have tried the both options but still its giving unhandled exception on debugger and application closes.
    11-03-2013 08:44 AM
  4. HG Software's Avatar
    Does the exception text say anything that may help or is there an inner exception in the exception that says anything helpful? Usually, these exceptions come from "bad" XAML, e.g. (static) resources that can't be found. I suggest that you comment out some XAML step by step until you know what line is to blame. One thing I just noticed is that you wrote Width="200px", try just Width="200" , I believe you can't specify units here.
    11-03-2013 09:02 AM
  5. Catholic Tech Geek's Avatar
    It looks like you want just a flat list and not a grouped list, correct? If so, you are missing two things. You need 2 more DataTemplate for the ListHeader property and ListFooter property of LongListSelector. I don't see anything wrong with your data class, but HG Software is correct in that you either need to set the ItemSource property of LongListSelector to your ObservableCollection (or List<T> works too, as long as the data structure implements IEnumerable<T>, but probably stick with ObservableCollection for now since you are already set up for it) in code or bind it via XAML.
    11-04-2013 11:09 AM
  6. Shan Ali Khan's Avatar
    problem is solved all code was fine except I wrote 200px instead of 200
    visual studio should tell it also anyway thanks
    11-05-2013 09:28 AM

Similar Threads

  1. Replies: 11
    Last Post: 11-17-2013, 11:52 AM
  2. How to merge albums in Xbox Music?
    By twelvetudors in forum Groove Music
    Replies: 1
    Last Post: 11-11-2013, 06:25 AM
  3. Tempt to get a nexus 5
    By John Balacano in forum Android Phones
    Replies: 8
    Last Post: 11-02-2013, 12:33 PM
  4. Uploading images in full resolution to skydrive from phone....
    By Siddhartha Vuriti1 in forum Nokia Lumia 520
    Replies: 2
    Last Post: 11-02-2013, 09:23 AM
LINK TO POST COPIED TO CLIPBOARD