开发者

New Window in a TabItem?

开发者 https://www.devze.com 2022-12-14 13:33 出处:网络
Is it possible to open another Window in a TabControl\'s TabItem? The reason I want to d开发者_JAVA百科o this is that if I have 5 TabItems in my TabControl, the one Window file I\'m coding all these

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

The reason I want to d开发者_JAVA百科o 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?


<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;


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消