c# - Reuse UserControl in TabControl with Various ViewModels -


i have 1 user control reuse multiple viewmodels, implement same interface. have these embedded in tabcontrol.

currently can single instance struggling reuse usercontrol. single instance can either bind viewmodel in usercontol's xaml or instantiate in code behind, can't figure out how set higher level.

here have,

      <tabcontrol horizontalalignment="left" height="800" margin="0,0,0,0" verticalalignment="top" width="600">         <tabitem header="tab1">             <frame source="someusercontrol.xaml"  borderthickness="0" margin="0" />         </tabitem>     </tabcontrol> 

here pseudo code achieve,

      <tabcontrol>         <tabitem header="tab1">             <frame source="{someusercontrol.xaml, datacontext=viewmodel1}" />         </tabitem>         <tabitem header="tab2">             <frame source="{someusercontrol.xaml, datacontext=viewmodel2}" />         </tabitem>     </tabcontrol> 

thanks!

instead of hard coding tabs, bind observablecollection of "item" class. i.e. like

class mytabitems : inotifypropertychanged {   public string header...   public object datacontext... } 

create an:

observablecollection<mytabitems> 

bind tabcontrol itemssource observablecollection. in item template tabcontrol bind header header property , datacontext datacontext property. source can hardcoded in template or can add property item class , bind if want more flexibility.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -