How to make Windows 10 pivot/tab headers full width of screen

后端 未结 1 1767
醉酒成梦
醉酒成梦 2021-01-26 04:21

How can I make the pivot/tabs header have each individual tab be the same width and stretch the full width of the pivot header and not extend off the screen (particularly for mo

1条回答
  •  被撕碎了的回忆
    2021-01-26 04:31

    How can I make the pivot/tabs header have each individual tab be the same width and stretch the full width

    This might not be able to implemented by just setting the style of pivot. But this can be easily implemented by ViewTreeHelper class. We can try to calculate the width of each header item by actualwidth of the pivot header and the total header items count. And set the width manually, then the header items will stretch the full width.

    Code example like follows:

          
           
                
                                        
                                    
                                    
                                        
                                            
                                        
                                    
                                    
                                        
                                            
                                                
                                                
                                            
                                            
                                                
                                                    
                                                        
                                                    
                                                
                                            
                                            
                                            
                                                
                                                    
                                                    
                                                
                                            
                                            
                                                
                                                    
                                                        
                                                    
                                                    
                                                        
                                                    
                                                
                                            
                                            
                                                
                                                    
                                                        
                                                    
                                                    
                                                        
                                                    
                                                
                                            
                                            
                                                
                                                    
                                                        
                                                    
                                                    
                                                        
                                                    
                                                
                                            
                                            
                                                
                                                    
                                                        
                                                    
                                                    
                                                        
                                                    
                                                
                                            
                                            
                                                
                                                    
                                                        
                                                    
                                                    
                                                        
                                                    
                                                
                                            
                                        
                                    
                                
                            
                        
                    
                
               
         
             
                 
             
             
         
         
             
                 
             
             
         
         
             
                 
             
             
         
     
    

    Code behind:

     private static IEnumerable FindVisualChildren(DependencyObject depObj) where T : DependencyObject
     {
         if (depObj != null)
         {
             for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
             {
                 DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                 if (child != null && child is T)
                 {
                     yield return (T)child;
                 }
    
                 foreach (T childOfChild in FindVisualChildren(child))
                 {
                     yield return childOfChild;
                 }
             }
         }
     }
    
     private void Page_Loaded(object sender, RoutedEventArgs e)
     {
         IEnumerable headerpanel = FindVisualChildren(CustomPivot);
         double totalwidth = headerpanel.ElementAt(0).ActualWidth;
         IEnumerable items = FindVisualChildren(CustomPivot);
         int headitemcount = items.Count();
         for (int i = 0; i < headitemcount; i++)
         {
             items.ElementAt(i).Width = totalwidth / headitemcount;
         }
     }
    

    For code of the custom user control TabHeader please reference the official sample.

    And the result:

    0 讨论(0)
提交回复
热议问题