views:

143

answers:

3

Hi everyone,

In WPF, Is there a simple way to stop TabItems in a TabControl from being repositioned when the selected TabItem changes? So that clicking on a TabItem would simply display its contents, but not reposition the TabItems as it usually does (by moving the selected TabItem to the bottom row of tabs if it wasn't there already).

Edit: To clarify, I do want the tabs to be displayed in multiple rows, I just don't want the tab headers to be repositioned when a TabItem from a row other than the bottom row is selected. I'd like the collection of headers to remain completely static, but for the contents of that TabItem to still be displayed when its header is clicked.

Thanks!

A: 

If you have Blend you can Edit a copy of the template for your tab control to remove this behavior I believe.

Mike B
I really appreciate the negative without so much as a comment. I just spent a day pair programming with Microsoft team members, including a member of the blend team, in which we discussed the lookless nature of WPF controls. We also overrode the behavior of an expander control by editing a copy of the control template in blend and then setting the edited template as the default template for that control. If this behavior of the tab control is hard coded then it is contrary to the guidelines published by Microsoft.
Mike B
A: 

Thanks everyone.

Kirn
A: 

I know its late but I had to figure this out today so here goes.

Basically you need to create your own control template for the tab control and use a different panel to contain your tab items.

Here is a simplified example using a WrapPanel.

<Style TargetType="TabControl" x:Key="MyTabControl">
<Setter Property="Template">
  <Setter.Value>
    <ControlTemplate TargetType="TabControl">
      <Grid SnapsToDevicePixels="true" KeyboardNavigation.TabNavigation="Local" HorizontalAlignment="Stretch">
        <Grid.RowDefinitions>
          <RowDefinition Height="Auto" />
          <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <WrapPanel x:Name="HeaderPanel" HorizontalAlignment="Center" VerticalAlignment="Top" IsItemsHost="true"
            Grid.Row="0" KeyboardNavigation.TabIndex="1" />
        <ContentPresenter Grid.Row="1" x:Name="PART_SelectedContentHost" ContentSource="SelectedContent" Margin="10" />
      </Grid>
    </ControlTemplate>
  </Setter.Value>
</Setter>

You then use this as follows

 <TabControl Style="{StaticResource MyTabControl}" ....

You can download the control template samples here

bic