c# - TabControl ItemTemplate with UserControl inside -


i have question using tabcontrol mvvm pattern. here parts of code:

<usercontrol>         <usercontrol.datacontext>             <vm:tabsviewmodel />         </usercontrol.datacontext>         <usercontrol.resources>             <resourcedictionary >                 <resourcedictionary.mergeddictionaries>                     <resourcedictionary source="../styles/converters.xaml"/>                 </resourcedictionary.mergeddictionaries>             </resourcedictionary>                 </usercontrol.resources>          <grid>             <grid.resources>                 <style x:key="tabitemstyle" targettype="tabitem">                     <setter property="header" value="{binding customer.lastname}"/>                     <setter property="content" value="{binding}"/>                 </style>                 <datatemplate x:key="tabitemheadertemplate">                     <textblock text="{binding}" fontweight="medium"/>                 </datatemplate>                 <datatemplate x:key="tabitemcontenttemplate" x:shared="false">                     <grid>                         <grid.columndefinitions>                             <columndefinition />                             <columndefinition />                         </grid.columndefinitions>                          <groupbox header="info">                             <stackpanel orientation="horizontal" grid.column="0">                                 <textblock text="{binding customer.fullname}" />                             </stackpanel>                         </groupbox>                          <groupbox header="finances" grid.column="1" >                             <controls:customerfinancesview datacontextchanged="customerfinancesview_datacontextchanged"> <controls:customerfinancesview.datacontext> <vm:financesviewmodel /> </controls:customerfinancesview.datacontext>                             </controls:customerfinancesview>                         </groupbox>                     </grid>                 </datatemplate>             </grid.resources>             <tabcontrol                 itemssource="{binding tabs, mode=twoway, updatesourcetrigger=propertychanged}"                  selecteditem="{binding currenttab, mode=twoway, updatesourcetrigger=propertychanged}"                 itemcontainerstyle="{staticresource tabitemstyle}"                 itemtemplate="{staticresource tabitemheadertemplate}"                 contenttemplate="{staticresource tabitemcontenttemplate}"/>           </grid>     </usercontrol> 

underlaying view model provide methods , commands add or remove tabs. so:

public class tabsviewmodel : interactablemodel {     observablecollection<tabmodel> _tabs = new observablecollection<tabmodel>();     public observablecollection<tabmodel> tabs     {                  {             return _tabs;         }         set          {             if (_tabs == value)                 return;              _tabs = value;             onpropertychanged("tabs");         }     }      private tabmodel _currenttab;     public tabmodel currenttab     {         { return _currenttab; }         set         {             if (_currenttab == value)                 return;              _currenttab = value;             onpropertychanged("currenttab");         }     }      public tabsviewmodel()     {         ...     }       void opentab(customer customer)     {       var existedtab = tabs.firstordefault(t=>t.customer.id == customer.id);       if (existedtab != null)       {         existedtab.refresh();         currenttab = existedtab;       }       else       {     var newtab = new tabmodel(customer);      tabs.add(newtab);      currenttab = newtab;       }     }      void removetab(object param)     {        var tabtoremove = tabs.elementatordefault((int)param);         if (tabtoremove != null)        {           tabs.remove(tabtoremove);        }     }  } 

}

as can see, content of each tabitem templated tabitemcontenttemplate datatemplate. datatemplate contains usercontrol inside. assume empty usercontrol vs template. when i'm open first tab - control ctor called, corresponding viewmodel creates, , datacontextchanged event raised. opening other tab, or reopening 1 doesn't affect on creating usercontrol - it's still control instance created first tab opening. same thing viewmodel (ctor not called) , event (not raising). please me find out way instantiate user control datatemplate every time i'm open tab (or every time i'm open new tab; both variants suits) , destroy every time i'm closing tab. i've read lot articles this, thing found use

x:shared=false  

on datatemplate. workaround isn't working me.

p.s. final goal provide tab-specific data datatemplate's inner usercontrol using mvvm pattern. (every tab stores lot information inside. information separated usercontrols it's own view model. want customize displaying data each tab controls binding current tab model. so: tabsviewmodel stores collection; tabmodel stores model, control1vm(model), control2vm(model), ...)


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -