WPF:限制TabControl中标题区域的最大宽度

时间:2017-05-01 18:16:40

标签: c# wpf xaml tabcontrol

我正在使用C#/ WPF制作用户界面。

我的UI在Tab Control的标题区域有一个按钮,方便用户使用。

问题是,如果添加了多个标签,最后一个标签(或者如果它有多行,其中一个标签)可以被这些按钮覆盖,如附图中的红色矩形:

Last tab

有没有办法限制TabControl标题区域的最大宽度,但保留TabItems的内容区域宽度?

当前XAML:

<Grid>
    <Grid HorizontalAlignment="Stretch">
        <TabControl BorderThickness="0,0,0,1" Margin="0,0,0,0">
             <TabItem MinWidth="130" Height="27" Content="Title" />
             <TabItem MinWidth="130" Height="27" Content="Title" />
             <TabItem MinWidth="130" Height="27" Content="Title" />
             <TabItem MinWidth="130" Height="27" Content="Title" />
             <TabItem MinWidth="130" Height="27" Content="Title" />
             <TabItem MinWidth="130" Height="27" Content="Title" />
             <TabItem MinWidth="130" Height="27" Content="Title" />
             <TabItem MinWidth="130" Height="27" Content="Title" />
        </TabControl>
    </Grid>
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
        <Button Margin="0,2,5,0" HorizontalAlignment="Left" Height="20" Width="22" VerticalAlignment="top" Content="+" />
        <Button Width="65" Height="25" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,0,1,1" Content="Close" />
    </StackPanel>
</Grid>

`

注意:最重要的是,我可以使用TabItems的负边距来溢出父级(TabControl)的宽度,但这可能是一个不好的解决方法。

请告诉我这种情况下的好方法。

1 个答案:

答案 0 :(得分:1)

您可以定义自己的TabControl样式,或者更具体地说是ControlTemplate,以插入&#34; ButtonPanel&#34;像这样:

<TabControl.Style>
                    <Style  TargetType="{x:Type TabControl}">
                        <Setter Property="OverridesDefaultStyle"
                                Value="True" />
                        <Setter Property="SnapsToDevicePixels"
                                Value="True" />
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type TabControl}">
                                    <Grid KeyboardNavigation.TabNavigation="Local">
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto" />
                                            <RowDefinition Height="*" />
                                        </Grid.RowDefinitions>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="Auto" />
                                        </Grid.ColumnDefinitions>
                                        <VisualStateManager.VisualStateGroups>
                                            <VisualStateGroup x:Name="CommonStates">
                                                <VisualState x:Name="Disabled">
                                                    <Storyboard>
                                                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                                                                      Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)">
                                                            <EasingColorKeyFrame KeyTime="0"
                                                                                 Value="#FFAAAAAA" />
                                                        </ColorAnimationUsingKeyFrames>
                                                    </Storyboard>
                                                </VisualState>
                                            </VisualStateGroup>
                                        </VisualStateManager.VisualStateGroups>
                                        <TabPanel x:Name="HeaderPanel"
                                                  Grid.Row="0"
                                                  Panel.ZIndex="1"
                                                  Margin="0,0,4,-1"
                                                  IsItemsHost="True"
                                                  KeyboardNavigation.TabIndex="1"
                                                  Background="Transparent" />
                                        <StackPanel x:Name="ButtonPanel" Grid.Column="1" Orientation="Horizontal">
                                            <Button Margin="0,2,5,0"
                                                    HorizontalAlignment="Left"
                                                    Height="20"
                                                    Width="22"
                                                    VerticalAlignment="top"
                                                    Content="+" />
                                            <Button Width="65"
                                                    Height="25"
                                                    HorizontalAlignment="Right"
                                                    VerticalAlignment="Top"
                                                    Margin="0,0,1,1"
                                                    Content="Close" />
                                        </StackPanel>
                                        <Border x:Name="Border"
                                                Grid.Row="1"
                                                Grid.ColumnSpan="2"
                                                BorderThickness="1"
                                                CornerRadius="2"
                                                KeyboardNavigation.TabNavigation="Local"
                                                KeyboardNavigation.DirectionalNavigation="Contained"
                                                KeyboardNavigation.TabIndex="2">
                                            <Border.Background>
                                                <LinearGradientBrush EndPoint="0.5,1"
                                                                     StartPoint="0.5,0">
                                                    <GradientStop Color="{DynamicResource ContentAreaColorLight}"
                                                                  Offset="0" />
                                                    <GradientStop Color="{DynamicResource ContentAreaColorDark}"
                                                                  Offset="1" />
                                                </LinearGradientBrush>
                                            </Border.Background>
                                            <Border.BorderBrush>
                                                <SolidColorBrush Color="{DynamicResource BorderMediumColor}" />
                                            </Border.BorderBrush>
                                            <ContentPresenter x:Name="PART_SelectedContentHost"
                                                              Margin="4"
                                                              ContentSource="SelectedContent" />
                                        </Border>
                                    </Grid>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </TabControl.Style>