1

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?

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

3 Comments

The code stack.Children.Add(new Image() { Source = new BitmapImage(new Uri("header.png")) }); is failing...
The error I am getting is: Additional information: 'The invocation of the constructor on type 'Scoreboard_Assistant.MainWindow' that matches the specified binding constraints threw an exception.' Line number '3' and line position '9'.
Figured it out, I needed UriKind.Relative

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.