views:

1093

answers:

5

In Windows Forms the default behaviour of a TabControl is to have the tabs spill out to a scrollable area if they consume too much space (MultiLine = false).

What is the best approach to achieving this behavior in WPF?

UPDATE

I was trying to find a solution using TabControl.ItemsPanel but it seems anything I put in there gets completely ignored, so for this reason I've gone the hard way and started with TabControl.Template which is mind boggling that we have to do it this way if it turns out to be the correct approach.

Extremely far from being complete, my starting solution to the problem is as follows.

<TabControl>
  <TabControl.Template>
    <ControlTemplate TargetType="{x:Type TabControl}">
      <DockPanel>
        <ScrollViewer DockPanel.Dock="Top"
                      HorizontalScrollBarVisibility="Auto"
                      VerticalScrollBarVisibility="Disabled">
          <StackPanel Orientation="Horizontal" IsItemsHost="True" />
        </ScrollViewer>
        <ContentPresenter ContentSource="SelectedContent" />
      </DockPanel>
    </ControlTemplate>
  </TabControl.Template>
  <TabItem Header="One">First</TabItem>
  <TabItem Header="Two">Second</TabItem>
  <TabItem Header="Three">Third</TabItem>
  <TabItem Header="Four">Fourth</TabItem>
  <TabItem Header="Five">Fifth</TabItem>
</TabControl>
A: 

Hey Brett, the easiest option is to set the ItemsPanelTemplate on the TabControl. I think the default is WrapPanel, hence the Multiline behaviour. Change it to StackPanel for example and maybe add a ScrollViewer.

Something like this (just coding this without VS)

<TabControl>

<TabControl.ItemsPanel>

<ItemsPanelTemplate>

<StackPanel Orientation="Horizontal"/>

</ItemsPanelTemplate>

</TabControl.ItemsPanel>

</TabControl>

hope that helps a bit...

ppx
Hi ppx, this is pretty much the same approach I was taking, though nothing I was putting into the template would make a difference, it would still come out the same, so now I've decided to try and replace the whole TabControl template, this is not what I had intended to do however and there _must_ be some sort of easier solution using the ItemsPanelTemplate. I'll post what I've been trying so far in a second.
Brett Ryan
A: 

Your solution to replace the template seems to be the best way to do this. The default panel for the TabItems is a TabPanel, and I don't see anything like a "should wrap" property on it.

The documentation contains an example of replacing the TabControl template with a different TabPanel:

http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.tabpanel.aspx

Douglas
A: 

i had the same problem few years ago, my solution was to limit the size of the header, and the panel that contains it, of course you need to make your own template like what you started, and also i need to implement some scrolling support so i put two repeat buttons at the left and right side of the scroll viewer.

my inspiration was a nice project from code project called IE tabs in wpf. it's old as wpf and works good

Chen Kinnrot
+3  A: 

In working to make a TabControl where the tabs are stacked vertically along the left, I found this solution for you:

http://www.blogs.intuidev.com/post/2010/02/10/TabControlStyling_PartThree.aspx

Pretty impressive stuff!

tyriker
I just realised you had posted the same link I only just discovered, it is a great article and thanks for the answer, only wish I saw it back in March :)
Brett Ryan
A: 

I know this is an older post, but I wanted to add another idea should others be searching this on the internet.

If you set the width of the tabpanel to something larger it will be (assuming this is not a tabpanel that allows the user to continue to add other tabs in it). If you have the user adding new tabs to the tab panel, then a scroll bar will need to be added.

Rick