Jason Axelrod
Jason Axelrod

Reputation: 7805

WPF TabItem Customized Headers in Code?

I know in WPF, if I want to create a TabItem with a custom header with an image and text its very simple in XAML:

<TabItem>
    <TabItem.Header>
        <StackPanel Orientation="Horizontal">
            <Image Source="header.png" />
            <TextBlock Text="Header" />
        </StackPanel>
    </TabItem.Header>
</TabItem>

However, I am not constructing my TabItems in XAML, I am doing it in code:

TabItem tab = new TabItem();
tab.Header = header;
tabControl.Items.Add(tab);

How would I program this custom header in code?

Upvotes: 1

Views: 2634

Answers (1)

bdimag
bdimag

Reputation: 963

Using your XAML example, your TabItem could be constructed in code like this:

var tab = new TabItem();
var stack = new StackPanel() { Orientation = Orientation.Horizontal };
stack.Children.Add(new Image() { Source = new BitmapImage(new Uri("header.png", UriKind.Relative)) });
stack.Children.Add(new TextBlock() { Text = "Header" });
tab.Header = stack;

Upvotes: 1

Related Questions