0

I'm trying to create a WPF application where the homepage (aka MainWindow) will contain several tabs.

I'm wondering what would be the best approach/architecture to encapsulate code specific to each tab instead of coding everything in the same place ?

NOTE

After some research I found UserControl class, but I've always thought that class was use in order to avoid re-coding the same logic over and over. In my case I would be using them once for every tab.

scharette
  • 9,437
  • 8
  • 33
  • 67
  • 1
    Use MVVM. Have a main viewmodel, which has a child viewmodel property for each tab (and maybe other child viewmodels as well). Use datatemplates to associate the child viewmodel types with views. Implement views as UserControls. – 15ee8f99-57ff-4f92-890c-b56153 May 31 '18 at 18:12
  • @EdPlunkett I see. Interesting approach. – scharette May 31 '18 at 18:16
  • re: your last question, you talked me into it. There's a place here for that answer (though it might be here already). There are people for whom it's legitimately not obvious. Not sure what's to be done though. – 15ee8f99-57ff-4f92-890c-b56153 Jun 01 '18 at 15:51
  • @EdPlunkett what do you mean exactly ? – scharette Jun 01 '18 at 15:53
  • I mean that question shouldn't have been downvoted to oblivion. – 15ee8f99-57ff-4f92-890c-b56153 Jun 01 '18 at 15:54
  • @EdPlunkett this is what Stack have become honestly. I'm done posting question on this site. Closing flag are so opinion based it's ironic. see that [answer](https://stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch-both-locally-and-remotely) I could easily argue that refering to git doc would be an really easy way to accomplish the question yet it gives a ton of reputation to someone that answered an obvious question. – scharette Jun 01 '18 at 15:58
  • Nothing to the quality of that answer btw just underlying a big problem with Stack in my opnion at the moment. All comes down on perception of the first few viewers with rep on your question... – scharette Jun 01 '18 at 15:59

1 Answers1

1

Depending on your preference and desired level of complexity, you can use MVVM or just create a UserControl for every tab to better distribute the code. Resulting in your window's xaml looking something like:

<TabItem x:Name="tab_peerView" Header="Peers (Lobby)" Visibility="Visible">
    <Views:SomeUserControl1 />
</TabItem>
<TabItem x:Name="tab_PrepCheckList" Header="Prep" Visibility="Visible">
    <Views:SomeUserControl2 x:Name="view_prep" />
</TabItem>

Doing this method you can also offload things like command bindings and respective Execute and CanExecute to the UserControls.

GHayes
  • 416
  • 6
  • 8