简体   繁体   中英

add a textblock just before first tabitem wpf

I want to have my company's name on the TabControl just before the first tab-item.

I have tried to add a TextBlock directly in TabControl. But it appears as if a new TabItem without any header is added just before the first TabItem and inside that newly created TabItem I get my company's name.

Here is the code:

<TabControl>
    <TextBlock Text="MyCompanyName" />
    <TabItem Header="FirstTabITem" />
    <TabItem Header="SecondTabITem" />
</TabControl>

Is this the limitation of WPF?

I believe what you're looking to do is overwrite the XAML surrounding the <TabPanel> in the default TabControl.Template . I've done this once in the past and it wasn't too bad.

If you have Blend you can easily create a copy of the TabControl.Template to modify, or you can find an example MSDN template here and work from that.

If you work from the MSDN example template, just wrap the <TabPanel> in something else such as a DockPanel , and add a <TextBlock> with your company name to it.

<Style TargetType="{x:Type TabControl}">

  ...

  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type TabControl}">
        <Grid KeyboardNavigation.TabNavigation="Local">
          <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
          </Grid.RowDefinitions>
          <DockPanel Grid.Row="0">
              <TextBlock Text="Company Name" DockPanel.Dock="Left" />
              <TabPanel Name="HeaderPanel" IsItemsHost="True" ... />
          </DockPanel>

          ...

        </Grid>
      </ControlTemplate>
    </Setter.Value>
 </Style>

Use this It might help You

        <TabItem Header="My Company Name" IsEnabled="False"/>
        <TabItem Header="FirstTabITem" />
        <TabItem Header="SecondTabITem" />
    </TabControl>

Edit the style of first tab item as given below.

 <TabControl Margin="20">
        <TabItem IsEnabled="False" >
            <TabItem.Style>
                <Style TargetType="TabItem">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <TextBlock Text="My Company" Margin="0,0,5,0"/>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </TabItem.Style>
        </TabItem>
    <TabItem Header="FirstTabITem" IsSelected="True"/>
    <TabItem Header="SecondTabITem" />
  </TabControl>

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