c# - Binding Pivot control with Observable Collection MVVM (windows phone 8) -


i'm new wp8 & mvvm. created wp8 app requests various bits of data once user has logged in. can't pivots header created dynamically , don't know if because i'm doing in binding, inotifypropertychanged, both or else!!

here have done far:

i've got global mainviewmodel defined in app.cs stored data returned @ login time.

once login successful , data has been loaded mainviewmodel, redirect test page contains pivot control , i'm trying create pivot items dynamically.

this xaml test page i.e. mainpivotpage.xaml , mainpivotviewmodel initialized defined local resource , set datacontext pivot control , don't know if i'm doing right i'm assigning "name" property header of pivotitem object stored in observable collection pivots. property name 1 of 2 properties have in class called pivot contains pivotid , 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 root grid page content 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 mainpivotviewmodel created, set pivots observable collection same observable collection stored in mainviewmodel contains data returned @ logon. can see assign property rather internal variable ensure trigger inotifypropertychanged (well..., think)

public class mainpivotviewmodel : baseviewmodel {     private observablecollection<pivot> _pivots = null;      public mainpivotviewmodel()     {         pivots = app.mainviewmodel.pivots;                 }      public observablecollection<pivot> pivots     {                 {             return _pivots;         }          set         {             if (_pivots != value) this.setproperty(ref this._pivots, value);         }     } } 

i use setproperty function contained in base class , used generate inotifypropertychanged event , allows me without having set property name every time need inotifypropertychanged event.

this code 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 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     {         { return _pivotid; }         set { if (_pivotid != value) this.setproperty(ref this._pivotid, value); }     }      public string name     {         { return _name; }         set { if (_name != value) this.setproperty(ref this._name, value); }     } } 

you may notice 1 inheriting basemodel. same code in baseviewmodel wanted keep 2 separate. i'm not event sure needed on pivot class, trying different scenario , left in now.

i don't know i'm doing wrong, no matter try can't "name" property displayed header's text. i'm pretty sure mainpivotviewmodel initialized when assigned local resource it's calling constructor correctly initializing observable collection that's far goes.

but displays absolutely nothing!

another thing i've noticed when put breakpoints in "set" in onpropertychanged method in baseviewmodel class, eventhandler null, no matter assume should not case, can't see i'm doing wrong.

i've numerous articles on stackoverflow , others , can't see i'm doing wrong? got ideas?

thanks.

problem resolved!!!

my code right along xaml wasn't!

steep , painful learning curve guess! anyway, found resolution after finding article on stackoverflow showed me way wrote xaml not appropriate.

i'll honest, don't understand why doesn't work way defined in short, have use headertemplate , itemtemplate in order display data correctly when binded viewmodel!

here post: databound pivot not loading first pivotitem in windows phone 8


Comments