d.lavysh
d.lavysh

Reputation: 1504

ListBox gets only 8 items from ItemSource

I'm developing the app on Windows phone. On View i have Grid with ListBox:

<Grid x:Name="ContentGrid"
              Grid.Row="1">
            <ListBox x:Name="TestListbox" 
                ItemsSource="{Binding History}"                    
                Margin="24,0"   
                SelectionChanged="GoToSelection" 
                ItemTemplate="{StaticResource HistoryDataTemplate}"
                >

            </ListBox>            
        </Grid>

History is ObservableCollection.

And HistoryDataTemplate looks like:

 <phone:PhoneApplicationPage.Resources>
        <DataTemplate x:Key="HistoryDataTemplate">
            <Grid>
                <HistoryControls:HistoryItem d:LayoutOverrides="Width" Margin="0,0,0,24"/>
            </Grid>
        </DataTemplate>
    </phone:PhoneApplicationPage.Resources>

i use constructor of HistoryItem for subscribing to PropertyChanged event:

     public HistoryItem()
            {
                InitializeComponent();
                base.Loaded+=(new RoutedEventHandler(this.HistoryControl_Loaded));
            }
 private void HistoryControl_Loaded(object sender, RoutedEventArgs e)
        {
            this._dataContext.PropertyChanged += new PropertyChangedEventHandler(this._dataContext_PropertyChanged);
        }

When i have 1-8 items all works correct, but for >8 items constuctor is called only 8 times.

Upvotes: 1

Views: 306

Answers (2)

Andy
Andy

Reputation: 6466

If there are only 8 items visible on the form then the constructor isn't being called for off screen items because the list is virtualizing them.

you can change this behaviour with the property

 <ListBox x:Name="TestListbox" VirtualizingStackPanel.IsVirtualizing="False"

Upvotes: 4

Related Questions