Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add margin to WPF TabControl tabs?

Tags:

c#

wpf

tabcontrol

I would like to add some margin to the left side of WPF TabControl's TabItems. If I use the straightforward Margin="4" approach, then the margin is applied to every TabItem, instead I want to move the whole tab item container a bit more to the right. For example, by default it looks like the tabs are 2 pixels to the right, I want to increase that so that I can fit a button to the left side of the tabs.

like image 531
Tower Avatar asked Feb 26 '12 09:02

Tower


1 Answers

Your will need to define a new template for the TabControl.

See the example template on MSDN

If you use this template you can change the margin of the HeaderPanel to achieve what you want.

If you need to see the real template being used, you can use Blend to extract the template. You can then modify that.

An alternative is to derive your own TabControl and modify the margin in code-behind, for example:

public class MyTabControl : TabControl
{
    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        var panel = Template.FindName("HeaderPanel", this) as FrameworkElement;

        if(panel != null)
        {
            panel.Margin = new Thickness(20,2,2,2);
        }
    }
}
like image 84
Phil Avatar answered Sep 21 '22 00:09

Phil