locked
LongListSelector ItemsSource not working RRS feed

  • Question

  • Also Can be seen at;

    http://stackoverflow.com/questions/29188982/longlistselector-itemssource-not-working

    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;
            }
    Thursday, March 26, 2015 9:06 PM

Answers

  • Hi reyou,

    With only code snippet I cannot successfully debugging the app, could you provide more information about your project for instance you could share a repro demo for us.

    Simply create a test project by your code, looks like the issue is on your data source binding.

    I've change the code like below, works fine now:

    //remove DataContext and ItemSource since I don't know how you implement this

    <phone:LongListSelector x:Name="list" VerticalAlignment="Stretch" RenderTransformOrigin="0.5,0.5" BorderBrush="Blue" BorderThickness="2" Margin="10,10,0,0"> <phone:LongListSelector.RenderTransform> <CompositeTransform/> </phone:LongListSelector.RenderTransform> <phone:LongListSelector.ItemTemplate> <DataTemplate> <ListBoxItem Margin="0,6,0,6"> <StackPanel Background="Blue"> <TextBlock Text="{Binding Title}" TextWrapping="NoWrap" Foreground="Black"/> </StackPanel> </ListBoxItem> </DataTemplate> </phone:LongListSelector.ItemTemplate> </phone:LongListSelector>

    In the backend cs file:

    InitializeComponent();

    Countries = GetCountries(); // manually binding the Countries to XAML. list.ItemsSource = Countries;

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, March 27, 2015 7:57 AM
    Moderator