4

I've used the ItemsPanelTemplate on other controls such as the ListBox, so I figured doing the same thing for the TabControl would be simple.

Apparently, I'm missing something and the TabControl is completely ignoring what I place in the ItemsPanelTemplate.

I have xaml that looks kinda of like this:

<TabControl TabStripPlacement="Right" ItemsSource="{Binding Components}">
     <TabControl.ItemsPanel>
          <ItemsPanelTemplate>
               <WrapPanel />
          </ItemsPanelTemplate>
     </TabControl.ItemsPanel>
</TabControl>

I've tried replacing WrapPanel with UniformGrid to see if there was a difference and it behaves the same. I do have a ControlTemplate for TabItem, but I tried removing it and it made no difference so I don't think that's effecting my problem.

Ashley Grenon
  • 9,305
  • 4
  • 41
  • 54
  • 1
    It's kind of weird to replace the ItemsPanel from a TabPanel to a WrapPanel... it makes no sense to do that. – myermian Nov 17 '11 at 19:38
  • well it's not my end goal to use a wrappanel there. i tried it and was mostly wondering why it didn't work like I thought it would. – Ashley Grenon Nov 17 '11 at 19:50

1 Answers1

6

You're probably looking to overwrite the Template, not the ItemsPanel

You can overwrite TabControl.ItemTemplate (or TabItem.Template) to alter the appearance of the Tabs along the top, TabControl.ContentTemplate to alter the template used for the content of the Tab, or TabControl.Template to alter the overall template of the TabControl.

I wasn't even aware that TabControl's had an ItemsPanel. I've only ever used that with an ItemsControl, where the ItemsPanel affects what kind of control contains the items in the collection. If the TabControl has that property, I expect it's only because it inherited it from some base class

Rachel
  • 130,264
  • 66
  • 304
  • 490
  • 1
    well I'm actually already using TabControl.ItemTemplate to alter the appearance of the tabs (I just posted a super simple piece of xaml), but what I really want to do is enclose all the tabs into a scrollviewer, because I have cases where there's too many tabs to display and they're running off the screen. – Ashley Grenon Nov 17 '11 at 19:35
  • @townsean I overwrite the `TabControl.Template` to do that. Use `IsItemsHost="True"` wherever you want your top Tabs to be (eg ``), and `` for wherever you want the currently selected Tab Content to go – Rachel Nov 17 '11 at 19:38
  • @Rachel: Correct, TabControl inherits from ItemsControl, which contains an ItemsPanel property. The TabControl sets this as a type of TabPanel. The user attempting to change it to a WrapPanel breaks the TabControl because the control was not designed to work with the WrapPanel, but instead a TabPanel (or a class that derives from it). – myermian Nov 17 '11 at 19:40
  • when i set ContentSource="SelectedContent", i get the msg: "Cannot bind properties of ContentPresenter because there is no property named 'SelectedContent' on type 'System.Windows.Controls.Control'." I'm placing the ContentPresenter inside of a StackPanel, which is inside of the ControlTemplate for the TabControl. ControlTemplates are my Achilles Heel @_@ – Ashley Grenon Nov 17 '11 at 19:55
  • @townsean Make sure your ControlTemplate has the TargetType defined, or it will default to a generic control template. `` – Rachel Nov 17 '11 at 19:59
  • Hi Rachel, but what if i wanted to support any Panel without the need to re-define the template each time. I tried to use a ContentPresenter to show ItemsPanel, but i couldn't make it work. It seems much harder than it should be. – Mauro Sampietro Sep 26 '18 at 07:53