2

I am using a TabControl to programmatically show or hide groups of form controls. I have implemented the technique described here and it approximately works as expected, except that there is a band approximately 1 or 2 pixels high in the location where the tab headers are normally displayed.

I have verified this by using Snoop to navigate the visual tree and observe the movement of the highlight rectangle as each element is selected. The size of the rectangle for the tab content element is fractionally smaller than that of the containing TabControl, which accounts for the extra pixels I am seeing. None of the elements that might affect this have margin, border or padding.

To achieve proper alignment with other controls, I need to eliminate this extra space, but I am not sure how. However, perhaps the question I should be asking is "is there a better way to selectively show / hide groups of controls?".

Thanks for your ideas, Tim

Community
  • 1
  • 1
Tim Coulter
  • 8,705
  • 11
  • 64
  • 95
  • is it acceptable if you completely remove the tabcontrol in the code behind when it need to be hidden? – dnr3 Aug 04 '10 at 13:28
  • The TabControl is never completely hidden. One of its tabs is always visible, depending on which group of controls the app needs to display, so I don't think removing the tab control is an option. On the other hand replacing each of the tabs with some kind of generic panel control would be acceptable, but I am not sure which control is suitable for this. – Tim Coulter Aug 04 '10 at 14:12
  • 1
    owh sorry, i followed the link you posted, and i thought you want to remove the tab header completely. Actually if you want to show/hide groups of controls, you can just use grids and switch out their visibility (atleast that what i've done in my project when i need a different layout depends on the selected record type) – dnr3 Aug 04 '10 at 15:23

1 Answers1

2

I suppose the thin line is caused by the TabPanel which is still there even though all TabItems are collapsed.

However, you could change the TabControl's ControlTemplate and bind the TabPanel's Visibility to the number of tabs, like this:

<TabPanel ... Visibility="{Binding Items.Count, RelativeSource={RelativeSource FindAncestor, Type={x:Type TabControl}}, Converter={StaticResource ZeroToCollapsedConverter}}" ... />

Of course, you will have to implement a converter which converts 0 to Visibility.Collapsed and all other values to Visibility.Visible.

BTW: You can get the default ControlTemplate for the TabControl here.

gehho
  • 9,049
  • 3
  • 45
  • 59
  • Thanks - great solution. In fact, there will never be a situation with no tabs, so I didn't need to bind to the item count and create a value converter. Just a simple replacement of the control template with the TabPanel collapsed solved the problem. BTW that's a very useful resource for the default WPF control templates. – Tim Coulter Aug 04 '10 at 16:55
  • Oh, you're right about the "no tabs" thing. Of course, I meant 1 tab and not 0. So, how did you solve it now? No converter? But then you will never have a `TabPanel`, will you? Maybe I misunderstood your question slightly? Just curious about your solution... And regarding the default templates: I also once looked for this a *looong time* and was pretty happy that I found it, that's why I share it with as many people as I can. :) – gehho Aug 05 '10 at 09:12
  • Oh, I just saw that it was the linked question which wanted the `TabPanel` to be invisible if only one tab is displayed. I realized that you did not want to display it at all, so this situation is even easier, as you already found out. :) – gehho Aug 05 '10 at 09:15