Note that there are some explanatory texts on larger screens.

plurals
  1. POWP7 listbox binding not working properly
    primarykey
    data
    text
    <p>A noob error for sure (I started yesterday afternoon developing in WP7), but I'm wasting a lot time on it.<br> I post my class and a little part of my code:</p> <pre><code>public class ChronoLaps : INotifyPropertyChanged { private ObservableCollection&lt;ChronoLap&gt; 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&lt;ChronoLap&gt; Laps { get { return laps; } set { if (value == laps) return; laps = value; if (laps != null) { laps.CollectionChanged += delegate { MeanTime = Laps.Sum(p =&gt; p.Time.TotalMilliseconds) / (Laps.Count * 1000); NotifyPropertyChanged("MeanTime"); }; } NotifyPropertyChanged("Laps"); } } } </code></pre> <p><strong>MainPage.xaml.cs</strong></p> <pre><code>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; } } </code></pre> <p><strong>MainPage.xaml</strong></p> <pre><code>&lt;phone:PhoneApplicationPage&gt; &lt;Grid x:Name="LayoutRoot" Background="Transparent"&gt; &lt;Grid Grid.Row="2"&gt; &lt;ScrollViewer Margin="-5,13,3,36" Height="758"&gt; &lt;ListBox Name="lbHistory" ItemContainerStyle="{StaticResource ListBoxStyle}" ItemsSource="{Binding Laps}" HorizontalAlignment="Left" Margin="5,25,0,0" VerticalAlignment="Top" Width="444"&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Text="{Binding Lap}" Width="40" /&gt; &lt;TextBlock Text="{Binding Time}" Width="140" /&gt; &lt;TextBlock Text="{Binding TotalTime}" Width="140" /&gt; &lt;TextBlock Text="{Binding Distance}" /&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; &lt;/ScrollViewer&gt; &lt;/Grid&gt; &lt;/Grid&gt; &lt;/phone:PhoneApplicationPage&gt; </code></pre> <p>Problem is that when I add one or more items to <code>History.Laps</code> collection, my listbox is not refreshed and these items don't appear.<br> But if I remove comment on <code>// History.Laps.Add(new ChronoLap()...</code> line, this item appear and so every other inserted later.<br> More: if I remove that comment and then write <code>History.Laps.Clear()</code> (before or after setting binding) binding is not working anymore. It's like it gets crazy if collection is empty.<br> I really don't understand the reason...</p> <p><strong>UPDATE AND SOLUTION</strong>:<br> If i move</p> <pre><code>History = new ChronoLaps(); LayoutRoot.DataContext = History; </code></pre> <p>from <code>butStart_Click</code> to <code>public MainPage()</code> everything works as expected.<br> Can someone explain me the reason?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload