views:

108

answers:

1

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

+1  A: 

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
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
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
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