Question

Is it possible to open another Window in a TabControl's TabItem?

The reason I want to do this is that if I have 5 TabItems in my TabControl, the one Window file I'm coding all these TabItems and their associated actions will get very large. So it would be nice if it was possible to to give each TabItem its own Window file.

Or how do you solve the problem where theWindow file controlling the TabControl gets too large?

Was it helpful?

Solution

You have several choices:

  • add one or more resource dictionaries to your app that contain resources with templates and styles for the various views you host in your tabs. This approach works well if you just need to maintain separation of the visual trees only.
  • create user controls for each view (with own XAML and class file) and use one instance for each different view in the tabs. This approach allows you to encapsulated specific business logic and the corresponding visual tree together.
  • generate some of the UI from code. This one has no advantages, except t makes you XAML smaller. And is your .cs files become too big, you can always split them in multiple code files and use partial classes. (just had to throw in this one for completeness :-))

OTHER TIPS

<Window ...
     xmlns:local="clr-namespace:MyNamespace"
     >
     <TabControl>
          <TabItem Header="FirstTab">
               <local:MyFirstTabUserControl/>  
          </TabItem>
          <TabItem Header="SecondTab">
               <local:MySecondTabUserControl/>  
          </TabItem>
          <TabItem Header="ThirdTab">
               <local:MyThirdTabUserControl/>  
          </TabItem>
     </TabControl>
</Window>

Your each TabUserControl is actually simple UserControl, since TabItem can host any control as its own child.

You can also set the TabItem Content equals to your Window content

ex: [WindowType is your window]

WindowsType oWindow = new WindowType();
TabItem oTab = new TabItem();
oTab.Content = oWindow.Content;

Make a UserControl for each TabItem.

You can use a UserControl as was mentioned already.

But you can also use the Page control. Each "Window" would be a page, and the tab would have a NavigationControl in it. I've used this approach before and it works well. I prefer the UserControl route, but both are valid.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top