且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在WPF中设置动态创建的选项卡标题的高度?

更新时间:2023-01-05 10:03:14

Hi oistrez,



You are using an \"ItemTemplate\" (btw why ist the XAML not correct in you example = lowercase itemtemplate...)

So this is what controls the \"inside\" of you TabPage header.



You use just a TextBlock here - so you could set it’s Height just by Controlling the DataTemplate

like this



Hi oistrez,

You are using an "ItemTemplate" (btw why ist the XAML not correct in you example = lowercase itemtemplate...)
So this is what controls the "inside" of you TabPage header.

You use just a TextBlock here - so you could set it's Height just by Controlling the DataTemplate
like this

TabControl.ItemTemplate>
<DataTemplate>
 <TextBlock Text="{Binding DisplayName}" Height="50">
...





Another way would be to directly manipulate the ControlTemplate that is behind an TabItem (I hope you know how to generate them in VS -if you don’t see them in your Code base?)

So as example the one I get for the default (Windows-Theme) TabItem



Another way would be to directly manipulate the ControlTemplate that is behind an TabItem (I hope you know how to generate them in VS -if you don't see them in your Code base?)
So as example the one I get for the default (Windows-Theme) TabItem

      <Style x:Key="TabItemStyle1" TargetType="{x:Type TabItem}">
            <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
            <Setter Property="Foreground" Value="Black"/>
            <Setter Property="Background" Value="{StaticResource TabItem.Static.Background}"/>
            <Setter Property="BorderBrush" Value="{StaticResource TabItem.Static.Border}"/>
            <Setter Property="Margin" Value="0"/>
            <Setter Property="Padding" Value="6,2,6,2"/>
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="VerticalContentAlignment" Value="Stretch"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabItem}">
                        <Grid x:Name="templateRoot" SnapsToDevicePixels="true">
                            <Border x:Name="mainBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1,1,1,0" Background="{TemplateBinding Background}" Margin="0">
                                <Border x:Name="innerBorder" BorderBrush="{StaticResource TabItem.Selected.Border}" BorderThickness="1,1,1,0" Background="{StaticResource TabItem.Selected.Background}" Margin="-1" Opacity="0"/>
                            </Border>
                            <ContentPresenter x:Name="contentPresenter" ContentSource="Header" Focusable="False" HorizontalAlignment="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
                        </Grid>
...





You see the ContentPresenter which is responsable for showing the Header - you could replace this with anything you want...



I hope this helps,



Kind regards



Johannes



You see the ContentPresenter which is responsable for showing the Header - you could replace this with anything you want...

I hope this helps,

Kind regards

Johannes