0

In my Windows Phone 8.1 store app I am having trouble to show values in LongListSelector. Here is .xaml and .cs files.

Am I missing something?

<controls:LongListSelector Grid.Row="0" Grid.Column="0"  VerticalAlignment="Stretch" 
                                           DataContext="{Binding ElementName=PageWorld}"
                                           ItemsSource="{Binding Countries}"  RenderTransformOrigin="0.5,0.5" BorderBrush="Blue" BorderThickness="2">
                    <controls:LongListSelector.RenderTransform>
                        <CompositeTransform/>
                    </controls:LongListSelector.RenderTransform>
                    <controls:LongListSelector.ItemTemplate>
                        <DataTemplate>
                            <ListBoxItem Margin="0,6,0,6">
                                <StackPanel>
                                    <TextBlock Text="{Binding Title}" TextWrapping="NoWrap"  Foreground="Black"/>
                                </StackPanel>
                            </ListBoxItem>
                        </DataTemplate>
                    </controls:LongListSelector.ItemTemplate>
                </controls:LongListSelector>

In code behing I am binding values as follows.

private ObservableCollection<Country> _countries;

        public ObservableCollection<Country> Countries
        {
            get { return _countries; }
            set
            {
                _countries = value;
                OnPropertyChanged();
            }
        }

        public World()
        {
            InitializeComponent();
            navigationHelper = new NavigationHelper(this);
            navigationHelper.LoadState += this.NavigationHelper_LoadState;
            navigationHelper.SaveState += this.NavigationHelper_SaveState;
            Countries = GetCountries();
        }

 public class Country
        {
            public string Title { get; set; }
        }
        private ObservableCollection<Country> GetCountries()
        {
            ObservableCollection<Country> countries = new ObservableCollection<Country>();
            for (int i = 0; i < 100; i++)
            {
                Country country = new Country();
                country.Title = "Name" + i;
                countries.Add(country);
            }
            return countries;
        }
Teoman shipahi
  • 47,454
  • 15
  • 134
  • 158
  • You're changing the collection, not the items in the collection. I think the problem is there. – crea7or Mar 21 '15 at 23:27
  • What do you mean by changing the collection? – Teoman shipahi Mar 21 '15 at 23:32
  • It looks like something wrong with `PropertyChanged` in `Countries` property and `LongListSelector` has not rebounded to the new collection after change. Check [this answer](http://stackoverflow.com/questions/17996542/replace-entire-observablecollection-with-another-observablecollection). – crea7or Mar 21 '15 at 23:53
  • I am already imlementing INotifyPropertyChanged so it should trigger binding right? – Teoman shipahi Mar 22 '15 at 00:21
  • It should, but as you see it doesn't. Control is binded to the old collection. Try to use binding in code behind. – crea7or Mar 22 '15 at 00:25
  • Try binding the new collection in c# i.e. after `Countries = GestCountries()` insert the line `myLongListSelector.ItemsSource = Countries` This should remedy the issue if it is as crea70r says. – Ali250 Mar 22 '15 at 12:59
  • I would like to keep binding at xaml page. not from code behind. Cannot do that? – Teoman shipahi Mar 22 '15 at 14:40

0 Answers0