Click here to Skip to main content
12,395,871 members (62,608 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# Mobile Silverlight , +
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 stuff 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 4 days trying to figure this out with no results.

This is how I set DataContext in MainPage.xaml:
<phone:PhoneApplicationPage 
    blablabla="Irrelevant stuff"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    Loaded="PageLoaded">

and in usercontrol:
<UserControl 
    blablabla="Irrelevant stuff"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">

And I'd like to emphasize one thing: 3 userscontrols are displayed, so ListBox knows, that there are 3 items in my collection. The problem is, that usercontrols don't receive new values through binding. I have another usercontrol, and I've tried to set binding to the same collection and results were the same: controls are displayed, but don't receive new values.

However built-in Silverlight controls works fine. I can bind "Date" to TextBox and it's displayed properly. I can delete item template for ListBox and set DisplayMemberPath="Date" and it's displayed correctly.

Looks like that this problem is only with usercontrols. But I can't figure out why and I'm asking for your help.

BTW. I can't see any binding exceptions in output window.
Posted 22-Aug-12 0:48am
Andreju212
Updated 22-Aug-12 1:55am
v5
Comments
Christian Graus 22-Aug-12 7:21am
   
Windows phone would be a good tag to add, then

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 22 Aug 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100