简体   繁体   中英

New Window in a TabItem?

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 TabItem s in my TabControl , the one Window file I'm coding all these TabItem s 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 the Window file controlling the TabControl gets too large?

<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 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 :-))

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;

为每个TabItem创建一个UserControl。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM