如何从TabControl获取TabPanel ActualWidth

时间:2015-03-16 06:12:07

标签: wpf tabcontrol tabpanel

我有一个tabcontrol,标签条位于左侧。所以它是一个简单的导航菜单类型选项卡控件。 TabConnel的控件模板中有一个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(标签标题),tabcontrol外的ActualWidth用于对齐目的。

我尝试过但是没有用。

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

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

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

TabControls.Items集合提供了ViewModel对象的列表。所以我无法获得TabItem实例。

1 个答案:

答案 0 :(得分:0)

我最终在代码隐藏文件

中使用了可视树辅助工具
var tabPanel = VisualTreeHelpers.FindChild<TabPanel>(TabControl, "MenuPanel");