0
votes

Everything happens in Windows Phone 7.1 Dev environment.

In my MainPage.xaml I have the ListBox:

<ListBox x:Name="LottoListBox" ItemsSource="{Binding lottoResults}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <local:LottoResults Date="{Binding Date}" Results="{Binding Results}" />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

As you can see, as ItemSource I set "lottoResults" collection.

public ObservableCollection<Lotto> lottoResults { get; set; }

"Lotto" class:

public DateTime Date
{
    get { return _date; }
    set
    {
        _date = value;
        NotifyPropertyChanged("Date");
    }
}
private DateTime _date;

public Structures.DoubleResult[] Results
{
    get { return _results; }
    set
    {
        _results = value;
        NotifyPropertyChanged("Results");
    }
}
private Structures.DoubleResult[] _results;

public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(string propertyName = "")
{
    if(PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

DoubleResult structure contain two fields - int and bool, but it's not important right now because I didn't set any binding to "Results" field.

Let's look at my usercontrol "LottoResults":

public DateTime Date
{
    get { return (DateTime)GetValue(DateProperty); }
    set { SetValue(DateProperty, value); }
}

public static readonly DependencyProperty DateProperty =
    DependencyProperty.Register("Date", typeof(DateTime), typeof(LottoResults), new PropertyMetadata(new DateTime(), dateChanged));

public static void dateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    LottoResults control = d as LottoResults;
    MessageBox.Show("DateChanged!");
    // some magical, irrelevant voodoo which only I can understand. ;-)
}

And now some XAML, where I bind "Date" field.

<TextBlock Grid.ColumnSpan="6" Foreground="Black" FontSize="34" FontWeight="Bold" Text="{Binding Date, StringFormat='{}{0:yyyy.MM.dd}'}" />

And surprise, surprise - binding doesn't work! Well, in usercontrol it does. In DependencyProperty as a default value I set "new DateTime()", this is 0001.01.01 and exactly that is being displayed in the TextBlock.

In my lottoResults collection (ListBox's ItemsSource) I have 3 items (none of them has date "0001.01.01"). And ListBox displays 3 items, but a displayed date is always 0001.01.01. Even more, the "dateChanged()" method is never being executed (I get no MessageBox nor it stops on a breakpoint), so I guess that "Date" field never receives new value from binding.

But what is interesting, if I copy the code of TextBlock from usercontrol to the MainPage.xaml (so now it takes values directly from "lottoResults" collection) the binding does work!

<ListBox x:Name="LottoListBox" ItemsSource="{Binding lottoResults}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <TextBlock Foreground="Black" FontSize="34" FontWeight="Bold" Text="{Binding Date, StringFormat='{}{0:yyyy.MM.dd}'}" />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

So, why is that? I'm at the dead-end. I've spent half of a day to figure this out with no results.

1

1 Answers

0
votes

The Lotto class needs to implement INotifyPropertyChanged this will cause the UI/View to be signalled when a Lotto object property changes.

If the Results collection changes (items are added, removed) you'll need to use an ObservableCollection there too instead of an array.