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

C# WPF:限制TabControl中标题区域宽度的最大大小,c#,wpf,xaml,tabcontrol,C#,Wpf,Xaml,Tabcontrol,我正在使用C#/WPF来创建UI 为了方便用户,我的UI在选项卡控件的标题区域有一个按钮 问题是,如果添加了多个选项卡,最后一个选项卡(或者如果有多行,则其中一行)可能会被这些按钮覆盖,如所附图片中的红色矩形: 有没有办法限制TabControl标题区域的最大宽度,但保持TabItems内容区域的宽度 当前XAML: <Grid> <Grid HorizontalAlignment="Stretch"> <TabControl Border

我正在使用C#/WPF来创建UI

为了方便用户,我的UI在选项卡控件的标题区域有一个按钮

问题是,如果添加了多个选项卡,最后一个选项卡(或者如果有多行,则其中一行)可能会被这些按钮覆盖,如所附图片中的红色矩形:

有没有办法限制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)的宽度,但这可能是一个糟糕的解决方法


请告诉我在这种情况下什么是好方法。

您可以定义自己的TabControl样式,或者更具体地说是ControlTemplate,以插入如下“ButtonPanel”:

<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>