TabItem in a separate XAML

后端 未结 4 1778
萌比男神i
萌比男神i 2020-12-01 06:04

Is it possible to put a TabItem into a separate XAML and reference something like this:


     

         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-01 06:09

    If what you want to do is simply make the code more manageable then I would recommend defining each tab's data in a user control, but still have the TabItem in the main tab control.

    Let's assume that your original code was this:

    
        
            
                
            
        
    
    

    To make the code more manageable you could break the tab contents into a UserControl such as:

    
        
            
        
    
    

    And then use that user control in your TabControl like this:

        
            
                
            
        
    

    If you really want to include the TabItem in your user control then you can do that by first creating a user control, and then change the type of the user control to the type TabItem (make sure you change this in both the xaml root node and the code behind).

    This would leave you with a tab control that looks like this:

        
            
            
            
        
    

    And each TabItem1 'User Control' would be of type TabItem. Here is an example:

    
        
            
        
    
    

    And as I mentioned, be sure to change the code behind so that it extends TabItem instead of user control:

    public partial class TabItem1 : TabItem
    {
        public TabItem1()
        {
            InitializeComponent();
        }
    }
    

提交回复
热议问题