want to make scrollable tabs for a tabcontrol

后端 未结 6 835
故里飘歌
故里飘歌 2020-12-05 14:22

Say I have a tab control, and I have over 50 tabs, where there is no enough space to hold so many tabs, how make these tabs scrollable?

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 14:40

    For thoose who want to know how to make the scrollviewer scroll to the selected tab item.

    Add this event SelectionChanged="TabControl_SelectionChanged" to your TabControl.

    Then give a name like TabControlScroller to the ScrollViewer inside the template. You should end with something like this

    
          
              
                  
                      
                          
                          
                      
                      
                          
                      
                      
                  
              
          
         
    
    

    Then in code behind you just have to add this method :

    private void TabControl_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        TabControl tabControl = (TabControl)sender;
        ScrollViewer scroller = (ScrollViewer)tabControl.Template.FindName("TabControlScroller", tabControl);
        if (scroller != null)
        {
            double index = (double)(tabControl.SelectedIndex );
            double offset = index * (scroller.ScrollableWidth / (double)(tabControl.Items.Count));
            scroller.ScrollToHorizontalOffset(offset);
        }
    }
    

提交回复
热议问题