How to Stretch WPF Tab Item Headers to Parent Control Width

后端 未结 11 1961
花落未央
花落未央 2020-11-29 23:17

Is there a way in XAML to cause the tab item headers to stretch across the width of the tab control?

For example, I have three tabs: red, blue and green. If I have a

11条回答
  •  佛祖请我去吃肉
    2020-11-29 23:36

    I followed Charlie's suggestion and went on re-templating route. Here is a simple implementation of TabControl that divides available space equally among its TabItems, using UniformGrid:

    Control's XAML

    
    
        
            
            
        
    
        
            
        
    
        
            
                
                    
                        
                        
                    
                    
                        
                        
                    
    
                    
                    
                        
                    
                
                
                    
                        
                        
                        
                        
                        
                    
                    
                        
                        
                        
                        
                        
                        
                        
                        
                        
                    
                    
                        
                        
                        
                        
                        
                        
                        
                        
                        
                    
                    
                        
                    
                
            
        
    
    

    Control's Code-Behind

    using System.Windows.Controls;
    using System.Windows.Controls.Primitives;
    
    namespace YourNamespace.Views
    {
      /// 
      /// A TabControl with large tabs. 
      /// 
      public partial class BigTabsTabControl : TabControl
      {
        public BigTabsTabControl()
        {
          InitializeComponent();
        }
    
        public override void OnApplyTemplate()
        {
          base.OnApplyTemplate();
    
          if (this.Template != null)
          {
            UniformGrid X = this.Template.FindName("headerPanel", this) as UniformGrid;
            if (X != null) X.Columns = this.Items.Count;
          }
        }
      }
    }
    

    That's it. You can now add TabItems to this control and they'll adjust their width automatically. No need to specify Grid.Column for these TabItems either, they work fine without it, even at design time.

提交回复
热议问题