WP7 listbox binding not working properly

Posted by Marco on Stack Overflow See other posts from Stack Overflow or by Marco
Published on 2012-06-01T19:00:37Z Indexed on 2012/08/31 9:38 UTC
Read the original article Hit count: 212

A noob error for sure (I started yesterday afternoon developing in WP7), but I'm wasting a lot time on it.
I post my class and a little part of my code:

public class ChronoLaps : INotifyPropertyChanged
{
    private ObservableCollection<ChronoLap> laps = null;

    public int CurrentLap
    {
        get { return lap; }
        set
        {
            if (value == lap) return;
            // Some code here ....
            ChronoLap newlap = new ChronoLap()
            {
                // Some code here ...
            };
            Laps.Insert(0, newlap);

            lap = value;
            NotifyPropertyChanged("CurrentLap");
            NotifyPropertyChanged("Laps");
        }
    }

    public ObservableCollection<ChronoLap> Laps { 
        get { return laps; }
        set
        {
            if (value == laps) return;
            laps = value;
            if (laps != null)
            {
                laps.CollectionChanged += delegate
                {
                    MeanTime = Laps.Sum(p => p.Time.TotalMilliseconds) / (Laps.Count * 1000);
                    NotifyPropertyChanged("MeanTime");
                };
            }
            NotifyPropertyChanged("Laps");
        }
    }
}

MainPage.xaml.cs

public partial class MainPage : PhoneApplicationPage
{
    public ChronoLaps History { get; private set; }

    private void butStart_Click(object sender, EventArgs e)
    {
        History = new ChronoLaps();
        // History.Laps.Add(new ChronoLap() { Distance = 0 });

        LayoutRoot.DataContext = History;
    }
}

MainPage.xaml

<phone:PhoneApplicationPage>    
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid Grid.Row="2">
            <ScrollViewer Margin="-5,13,3,36" Height="758">
                <ListBox Name="lbHistory" ItemContainerStyle="{StaticResource ListBoxStyle}"
                         ItemsSource="{Binding Laps}" 
                         HorizontalAlignment="Left" Margin="5,25,0,0"
                         VerticalAlignment="Top" Width="444">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Lap}" Width="40" />
                                <TextBlock Text="{Binding Time}" Width="140" />
                                <TextBlock Text="{Binding TotalTime}" Width="140" />
                                <TextBlock Text="{Binding Distance}" />
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </ScrollViewer>
        </Grid>
    </Grid>
</phone:PhoneApplicationPage>       

Problem is that when I add one or more items to History.Laps collection, my listbox is not refreshed and these items don't appear.
But if I remove comment on // History.Laps.Add(new ChronoLap()... line, this item appear and so every other inserted later.
More: if I remove that comment and then write History.Laps.Clear() (before or after setting binding) binding is not working anymore. It's like it gets crazy if collection is empty.
I really don't understand the reason...

UPDATE AND SOLUTION:
If i move

History = new ChronoLaps();
LayoutRoot.DataContext = History;

from butStart_Click to public MainPage() everything works as expected.
Can someone explain me the reason?

© Stack Overflow or respective owner

Related posts about windows-phone-7

Related posts about data-binding