This might be a stupid question but I'm having trouble recreating the Hub page data binding method used by the "Hub App" template in my own Windows Phone app.
My XAML is bound to a viewmodel class that is defined as a public property of my Page object and it all works fine as long as I include the line:
this.DataContext = *viewmodel object here*
within the OnNavigatedTo() method.
If I comment out this line, no data is loaded at runtime. This might sound obvious BUT (and this is my question), the "Hub App" template never assigns an object to "this.DataContext" in any .xaml.cs file. The binding is only ever defined in the XAML. What am I missing?
UPDATE: Added xaml and xaml.cs
XAML
<Page
DataContext="{Binding Subject, RelativeSource={RelativeSource Mode=Self}}"
d:DataContext="{d:DesignData Source=SampleData/SubjectSampleData.xaml}">
XAML.CS
public sealed partial class BlankPage1 : Page
{
public BlankPage1()
{
this.InitializeComponent();
}
public Subject Subject { get; set; }
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.Parameter != null)
{
var subjectName = e.Parameter as string;
var subject = App.MainViewModel.Subjects.Single(item => item.Name == subjectName);
if (subject != null)
{
this.Subject = subject;
}
//this.DataContext = this.Subject;
}
}
}