WPF特殊TabItem宽度行为

时间:2018-11-07 15:50:46

标签: c# wpf width tabcontrol tabitem

我已经在CodeBehind(C#)中动态添加了TabItems的代码:

<TabControl x:Name="tabList" Grid.Column="2" HorizontalContentAlignment="Stretch"  Background="Transparent" SelectionChanged="tabList_SelectionChanged" IsSynchronizedWithCurrentItem="True">
            <TabControl.Resources>
                <DataTemplate x:Key="TabHeader" DataType="TabItem">
                    <DockPanel>
                        <Button x:Name="btnDelete" BorderThickness="0" Background="Transparent" DockPanel.Dock="Right" Margin="5,0,0,0" Padding="0" Click="btnDelete_Click" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}}, Path=Name}">
                            <Image Source="myImage></Image>
                        </Button>
                        <ToggleButton x:Name="btnPin" Checked="btnDisablex" Unchecked="btnDisablex" BorderThickness="0" Background="Transparent" DockPanel.Dock="Right" Margin="5,0,0,0" Padding="0" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}}, Path=Name}">
                            <ToggleButton.Style>
                                //Many Useless Things
                            </ToggleButton.Style>
                        </ToggleButton>
                        <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}}, Path=Header}" />
                    </DockPanel>
                </DataTemplate>
                <Style TargetType="TextBox">
                     //Other Many Useless Things
                </Style>
            </TabControl.Resources>
        </TabControl>

并且在用户资源中具有以下样式:

<Style TargetType="{x:Type TabItem}">
        <Setter Property="AllowDrop" Value="True"/>
        <EventSetter Event="PreviewMouseMove" Handler="TabItem_PreviewMouseMove"/>
        <EventSetter Event="Drop" Handler="TabItem_Drop"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TabItem}">
                    <Grid>
                        <Border 
                            Name="Border"
                            Margin="0,0,-4,0" 
                            Background="White"
                            BorderBrush="#888" 
                            BorderThickness="1,1,1,1" 
                            CornerRadius="2,12,0,0">
                            <ContentPresenter x:Name="ContentSite"
                                VerticalAlignment="Center"
                                HorizontalAlignment="Center"
                                ContentSource="Header"
                                Margin="12,2,12,2"
                                RecognizesAccessKey="True"/>
                        </Border>
                    </Grid>
                    <ControlTemplate.Triggers>
                        //Many Useless MultiTrigger and Datatrigger
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style> 

代码工作正常,直到我添加更多选项卡并且窗口宽度不再足够,但是TabControl行为很奇怪:TabItem宽度变大并且第一个TabItems消失了。

我只希望TabItem的宽度减小,或者只是希望长者“消失”但始终保持相同的宽度。为什么这不起作用?

这是在正常情况下拍摄的屏幕截图

Normal view working OK

这里很明显的问题是:当我添加更多标签(或减小窗口/网格宽度)时,标签宽度变大

Tab View Problem

0 个答案:

没有答案