Unable to bind long list selector

Shan Ali Khan

New member
Oct 14, 2013
184
0
0
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:
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;
    }

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>
 
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. :wink:
 
I have tried the both options but still its giving unhandled exception on debugger and application closes.
 
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.
 
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.
 
problem is solved all code was fine except I wrote 200px instead of 200
visual studio should tell it also anyway thanks
 

Members online

No members online now.

Forum statistics

Threads
335,544
Messages
2,258,548
Members
428,741
Latest member
Droco3