I'm new to WP8 & MVVM. I created wp8 app which requests various bits of data once a user has logged in. I just can't get my pivots header to get created dynamically and I don't know if it is because I'm doing something in the binding, the INotifyPropertyChanged, both or something else!!
Here is what I have done so far:
I've got a global MainViewModel defined in App.cs which will stored all the data returned at login time.
Once the login is successful and the data has been loaded into the MainViewModel, I redirect this to a test page which contains a Pivot Control and I'm trying to create Pivot Items dynamically.
This is the xaml for my test page i.e. MainPivotPage.xaml and my MainPivotViewModel is initialized as it is defined as a local resource and is set as the datacontext for the Pivot control and I don't know if I'm doing this right but I'm assigning the "Name" property to the Header of the PivotItem which are the object stored in my observable collection Pivots. The property Name is one of the 2 properties I have in a Class called Pivot which contains PivotId and Name.
<phone:PhoneApplicationPage
x:Class="TestApp.Views.MainPivotPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModel="clr-namespace:TestApp.ViewModels"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True" Loaded="PhoneApplicationPage_Loaded">
<!--LayoutRoot is the root grid where all page content is placed-->
<phone:PhoneApplicationPage.Resources>
<viewModel:MainPivotViewModel x:Key="MainPivotViewModel" />
</phone:PhoneApplicationPage.Resources>
<Grid x:Name="LayoutRoot" Background="Transparent">
<!--Pivot Control-->
<phone:Pivot Title="My Search Options" x:Name="MainPivots" ItemsSource="{Binding Pivots}" DataContext="{StaticResource MainPivotViewModel}">
<phone:PivotItem Header="{Binding Name}">
<!--<Grid/>-->
</phone:PivotItem>
</phone:Pivot>
</Grid>
</phone:PhoneApplicationPage>
When the MainPivotViewModel is created, I set the Pivots observable collection to the same observable collection stored in my MainViewModel which contains all the data returned at logon. As you can see I assign it to the property rather than the internal variable to ensure that it will trigger the INotifyPropertyChanged (well..., I think)
public class MainPivotViewModel : BaseViewModel
{
private ObservableCollection<Pivot> _pivots = null;
public MainPivotViewModel()
{
Pivots = App.MainViewModel.Pivots;
}
public ObservableCollection<Pivot> Pivots
{
get
{
return _pivots;
}
set
{
if (_pivots != value) this.SetProperty(ref this._pivots, value);
}
}
}
I use the SetProperty function which is contained in my base class and is used to generate the INotifyPropertyChanged event and allows me to do so without having the set the property name every time I need to the INotifyPropertyChanged event.
This is the code for my BaseView:
public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
{
if (object.Equals(storage, value)) return false;
storage = value;
this.OnPropertyChanged(propertyName);
return true;
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var eventHandler = this.PropertyChanged;
if (eventHandler != null)
{
eventHandler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
My Pivot class looks like this:
public class Pivot: BaseModel
{
private int _pivotId;
private string _name = string.Empty;
public Pivot()
{
}
public Pivot(int pivotId, string name)
{
PivotId = pivodId;
Name = name;
}
public int PivotId
{
get { return _pivotId; }
set { if (_pivotId != value) this.SetProperty(ref this._pivotId, value); }
}
public string Name
{
get { return _name; }
set { if (_name != value) this.SetProperty(ref this._name, value); }
}
}
You may notice that this one is inheriting from BaseModel. This is exactly the same code as in BaseViewModel but I wanted to keep the two separate. I'm not event sure this is needed on my Pivot class, but I was trying different scenario and left it in for now.
I don't know what I'm doing wrong, but no matter what I try I can't get the "Name" property to be displayed as the Header's text. I'm pretty sure the MainPivotViewModel is initialized when it is assigned as a local resource as I it's calling my constructor correctly which is then initializing my observable collection but that's as far as it goes.
But it displays absolutely nothing!
Another thing I've noticed is when I put breakpoints in the "Set" in OnPropertyChanged method in the BaseViewModel class, the eventHandler is always null, no matter what which I assume should not be the case, but I can't see what I'm doing wrong.
I've numerous articles on stackoverflow and others and I just can't see what I'm doing wrong? Anyone got any ideas?
Thanks.