如何从 TabControl 获取 TabPanel ActualWidth

How to get TabPanel ActualWidth from TabControl

我有一个选项卡控件,选项卡条放置在左侧。所以它是一个简单的导航菜单类型的选项卡控件。 TabControl的控件模板里面有一个TabPanel。

<ControlTemplate TargetType="{x:Type TabControl}">
       <Grid x:Name="Grid" ClipToBounds="true" SnapsToDevicePixels="true" KeyboardNavigation.TabNavigation="Local">
               <Grid.ColumnDefinitions>
                   <ColumnDefinition x:Name="ColumnDefinition0"/>
                   <ColumnDefinition x:Name="ColumnDefinition1" Width="0"/>
               </Grid.ColumnDefinitions>
               <Grid.RowDefinitions>
                   <RowDefinition x:Name="RowDefinition0" Height="Auto"/>
                   <RowDefinition x:Name="RowDefinition1" Height="*"/>
               </Grid.RowDefinitions>
               <Border x:Name="ContentPanel" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="0" KeyboardNavigation.DirectionalNavigation="Contained" Grid.Row="1" KeyboardNavigation.TabIndex="2" KeyboardNavigation.TabNavigation="Local" Padding="{TemplateBinding Padding}" Margin="0,-1,0,0">
                   <ContentPresenter x:Name="PART_SelectedContentHost" ContentSource="SelectedContent" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
               </Border>
               <ScrollViewer x:Name="scroll" VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Auto" Template="{StaticResource ScrollViewerTab}">
                   <TabPanel x:Name="HeaderPanel" IsItemsHost="true" Margin="0,2,0,0" KeyboardNavigation.TabIndex="1" Panel.ZIndex="1" VerticalAlignment="Bottom" />
               </ScrollViewer>
           </Grid>
    </ControlTemplate>

我需要 TabPanel 的(tab headers)ActualWidth 在 tabcontrol 之外用于对齐目的。

我试过跟随,但没用。

<TextBlock Text="{Binding Path=Actualwidth, ElementName=HeaderPanel}" />
<TabControl />

我还尝试将 header 面板的 ActualWidth 绑定到 TabControl 的标签 属性。即使这样也行不通。

<TabControl Tag="{Binding Path=ActualWidth, ElementName=HeaderPanel}" />

TabControls.Items collection给出了ViewModel的列表objects。所以我无法获取 TabItem 实例。

我最终在代码隐藏文件中使用了可视树助手

var tabPanel = VisualTreeHelpers.FindChild<TabPanel>(TabControl, "MenuPanel");