简体   繁体   中英

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?

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM