Unable to bind long list selector

Shan Ali Khan

New member
Oct 14, 2013
184
0
0
Visit site
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>
 

HG Software

New member
Oct 25, 2013
25
0
0
Visit site
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:
 

HG Software

New member
Oct 25, 2013
25
0
0
Visit site
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.
 

Catholic Tech Geek

New member
Feb 2, 2012
130
0
0
Visit site
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.
 

Members online

Forum statistics

Threads
323,270
Messages
2,243,557
Members
428,052
Latest member
fopaky