Cys_Control(四) MTabControl
阅读原文时间:2023年07月10日阅读:1

¨C8C ¨C9C ¨C10C ¨C11C ¨C12C ¨C13C ¨C14C ¨C15C ¨C16C ¨C17C ¨C18C ¨C19C ¨C20C ¨C21C ¨C22C ¨C23C ¨C24C ¨C25C ¨C26C ¨C27C ¨C28C ¨C29C

分析原有样式

由上图可知TabControl由TabPanel和ContentPresenter两部分组成,并被一个两行两列的Grid包裹

分析上图触发器部分,TabStripPlacement控制着TabPanel和ContentPresenter 在网格中的位置

接着查看TabItem原有样式

¨C47C ¨C48C ¨C49C ¨C50C ¨C51C ¨C52C ¨C53C ¨C54C ¨C55C ¨C56C ¨C57C ¨C58C ¨C59C ¨C60C ¨C61C ¨C62C ¨C63C ¨C64C ¨C65C ¨C66C ¨C67C ¨C68C ¨C69C ¨C70C ¨C71C ¨C72C ¨C73C ¨C74C ¨C75C ¨C76C ¨C77C ¨C78C ¨C79C ¨C80C ¨C81C ¨C82C ¨C83C ¨C84C ¨C85C ¨C86C ¨C87C ¨C88C ¨C89C ¨C90C ¨C91C ¨C92C ¨C93C ¨C94C ¨C95C ¨C96C ¨C97C ¨C98C ¨C99C ¨C100C ¨C101C ¨C102C ¨C103C ¨C104C ¨C105C ¨C106C ¨C107C ¨C108C ¨C109C ¨C110C ¨C111C ¨C112C ¨C113C ¨C114C ¨C115C ¨C116C ¨C117C ¨C118C ¨C119C ¨C120C ¨C121C ¨C122C ¨C123C ¨C124C ¨C125C ¨C126C ¨C127C ¨C128C ¨C129C ¨C130C ¨C131C ¨C132C ¨C133C ¨C134C

TabItem由ContentPresenter和两个Border组成(内外边框)

去除内边框并简化触发器

<Style x:Key="MTabControl.TabItemStyle" TargetType="{x:Type TabItem}">  
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>  
    <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MTabControl}},Path=HeaderForeground}"/>  
    <Setter Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MTabControl}},Path=HeaderBorderBrush}"/>  
    <Setter Property="Background" Value="{DynamicResource ColorBrush.TabControlHeaderBackground}"/>  
    <Setter Property="HorizontalContentAlignment" Value="Center"/>  
    <Setter Property="VerticalContentAlignment" Value="Center"/>  
    <Setter Property="MinWidth" Value="100"/>  
    <Setter Property="MinHeight" Value="40"/>  
    <Setter Property="FontSize" Value="16"/>  
    <Setter Property="Template">  
        <Setter.Value>  
            <ControlTemplate TargetType="{x:Type TabItem}">  
                <Grid SnapsToDevicePixels="true">  
                    <Grid SnapsToDevicePixels="true">  
                        <Border x:Name="PART\_Border" Background="{TemplateBinding Background}"  
                                BorderThickness="0,0,0,2" BorderBrush="{TemplateBinding BorderBrush}" Padding="{TemplateBinding Padding}">  
                            <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}}}"/>  
                        </Border>  
                    </Grid>  
                </Grid>  
                <ControlTemplate.Triggers>  
                    <Trigger Property="IsSelected" Value="true">  
                        <Setter Property="Panel.ZIndex" Value="1"/>  
                        <Setter Property="Background" TargetName="PART\_Border" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MTabControl}},Path=HeaderBackground}"/>  
                        <Setter Property="Foreground" Value="{DynamicResource ColorBrush.FontPrimaryColor}"/>  
                    </Trigger>  
                    <Trigger Property="TabStripPlacement" Value="Bottom">  
                        <Setter Property="BorderThickness" TargetName="PART\_Border" Value="0,2,0,0"/>  
                    </Trigger>  
                    <Trigger Property="TabStripPlacement" Value="Left">  
                        <Setter Property="BorderThickness" TargetName="PART\_Border" Value="0,0,2,0"/>  
                    </Trigger>  
                    <Trigger Property="TabStripPlacement" Value="Right">  
                        <Setter Property="BorderThickness" TargetName="PART\_Border" Value="2,0,0,0"/>  
                    </Trigger>  
                </ControlTemplate.Triggers>  
            </ControlTemplate>  
        </Setter.Value>  
    </Setter>  
</Style>

<Style TargetType="{x:Type local:MTabControl}">  
    <Setter Property="Foreground" Value="{DynamicResource ColorBrush.FontDefaultColor}"/>  
    <Setter Property="Padding" Value="0"/>  
    <Setter Property="BorderThickness" Value="0"/>  
    <Setter Property="BorderBrush" Value="{DynamicResource ColorBrush.PrimaryBorderBrushColor}"/>  
    <Setter Property="Background" Value="{DynamicResource ColorBrush.FontDefaultColorV3}"/>  
    <Setter Property="HorizontalContentAlignment" Value="Center"/>  
    <Setter Property="VerticalContentAlignment" Value="Center"/>  
    <Setter Property="ItemContainerStyle" Value="{StaticResource MTabControl.TabItemStyle}"/>  
    <Setter Property="Template">  
        <Setter.Value>  
            <ControlTemplate TargetType="{x:Type local:MTabControl}">  
                <Border BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" >  
                    <Grid ClipToBounds="true" SnapsToDevicePixels="true" KeyboardNavigation.TabNavigation="Local">  
                        <Grid.ColumnDefinitions>  
                            <ColumnDefinition x:Name="ColumnDefinition0"/>  
                            <ColumnDefinition x:Name="ColumnDefinition1" Width="0"/>  
                        </Grid.ColumnDefinitions>  
                        <Grid.RowDefinitions>  
                            <RowDefinition x:Name="RowDefinition0" Height="Auto"/>  
                            <RowDefinition x:Name="RowDefinition1" Height="\*"/>  
                        </Grid.RowDefinitions>  
                        <UniformGrid x:Name="HeaderPanel" HorizontalAlignment="Stretch" IsItemsHost="true" Rows="1"  Panel.ZIndex="1"  />  
                        <Border x:Name="ContentPanel"  
                                Background="{TemplateBinding Background}"  
                                BorderBrush="{TemplateBinding BorderBrush}"  
                                Grid.Column="0"  
                                Grid.Row="1"  
                                KeyboardNavigation.DirectionalNavigation="Contained"  
                                KeyboardNavigation.TabNavigation="Local"  
                                KeyboardNavigation.TabIndex="2">  
                            <ContentPresenter x:Name="PART\_SelectedContentHost" ContentSource="SelectedContent"  
                                              Margin="{TemplateBinding Padding}"  
                                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>  
                        </Border>  
                    </Grid>  
                </Border>  
                <ControlTemplate.Triggers>  
                    <Trigger Property="TabStripPlacement" Value="Bottom">  
                        <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="1"/>  
                        <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>  
                        <Setter Property="Height" TargetName="RowDefinition0" Value="\*"/>  
                        <Setter Property="Height" TargetName="RowDefinition1" Value="Auto"/>  
                    </Trigger>  
                    <Trigger Property="TabStripPlacement" Value="Left">  
                        <Setter Property="Columns" TargetName="HeaderPanel" Value="1"/>  
                        <Setter Property="Rows" TargetName="HeaderPanel" Value="0"/>  
                        <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="0"/>  
                        <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>  
                        <Setter Property="Grid.Column" TargetName="HeaderPanel" Value="0"/>  
                        <Setter Property="Grid.Column" TargetName="ContentPanel" Value="1"/>  
                        <Setter Property="Width" TargetName="ColumnDefinition0" Value="Auto"/>  
                        <Setter Property="Width" TargetName="ColumnDefinition1" Value="\*"/>  
                        <Setter Property="Height" TargetName="RowDefinition0" Value="\*"/>  
                        <Setter Property="Height" TargetName="RowDefinition1" Value="0"/>  
                    </Trigger>  
                    <Trigger Property="TabStripPlacement" Value="Right">  
                        <Setter Property="Columns" TargetName="HeaderPanel" Value="1"/>  
                        <Setter Property="Rows" TargetName="HeaderPanel" Value="0"/>  
                        <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="0"/>  
                        <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>  
                        <Setter Property="Grid.Column" TargetName="HeaderPanel" Value="1"/>  
                        <Setter Property="Grid.Column" TargetName="ContentPanel" Value="0"/>  
                        <Setter Property="Width" TargetName="ColumnDefinition0" Value="\*"/>  
                        <Setter Property="Width" TargetName="ColumnDefinition1" Value="Auto"/>  
                        <Setter Property="Height" TargetName="RowDefinition0" Value="\*"/>  
                        <Setter Property="Height" TargetName="RowDefinition1" Value="0"/>  
                    </Trigger>  
                </ControlTemplate.Triggers>  
            </ControlTemplate>  
        </Setter.Value>  
    </Setter>  
</Style>

gitee地址:https://gitee.com/sirius_machao/Cys_Controls/tree/dev/