Reputation: 1504
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
Reputation: 1504
I added ListBox.ItemsPanel and it helps. More info: http://blogs.msdn.com/b/oren/archive/2010/11/08/wp7-silverlight-perf-demo-1-virtualizingstackpanel-vs-stackpanel-as-a-listbox-itemspanel.aspx
Upvotes: 1
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