在 tabcontrol FLAT (WPF) 中制作 TabItems

发布于 2024-12-18 01:14:42 字数 87 浏览 2 评论 0原文

我的 WPF 应用程序中有一个 tabControl。我想让选项卡变得平坦。当我将鼠标悬停在它们上方时,它们不能改变。

你能帮我解决这个问题吗?

I have a tabControl in my WPF application. I want to make tabitems flat.When I hover over them they must not change.

Can you help me about this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半﹌身腐败 2024-12-25 01:14:42

不知道这是否正是您所需要的,但您可以将此数据模板和样式添加到资源字典中,以使用平面样式格式化 TabControl ...

    <ControlTemplate x:Key="TabItemTemplate" TargetType="{x:Type TabItem}">
    <Border Cursor="Hand" x:Name="tab" Background="White" BorderThickness="1,1,1,0" BorderBrush="Black" Padding="5">
        <Grid>
            <ContentPresenter   x:Name="contentPresenter" ContentSource="Header"
                                TextBlock.Foreground="Black"
                                VerticalAlignment="Center"
                                HorizontalAlignment="Left"/>
        </Grid>
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="IsSelected" Value="False">
            <Setter TargetName="tab" Property="Background" Value="Gray"/>
            <Setter TargetName="tab" Property="BorderBrush" Value="Black"/>
        </Trigger>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsSelected" Value="False" />
                <Condition Property="IsMouseOver" Value="True"/>
            </MultiTrigger.Conditions>
            <MultiTrigger.Setters>
                <Setter TargetName="tab" Property="BorderBrush" Value="White"/>
                <Setter TargetName="tab" Property="BorderThickness" Value="1,1,1,0"/>
            </MultiTrigger.Setters>
        </MultiTrigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

<Style TargetType="{x:Type TabControl}">
    <Setter Property="Background" Value="White"/>
    <Setter Property="BorderBrush" Value="Black"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabControl}">
                <Grid ClipToBounds="true" SnapsToDevicePixels="true" KeyboardNavigation.TabNavigation="Local">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <TabPanel x:Name="HeaderPanel" IsItemsHost="true" Margin="2,2,2,0" Grid.Row="0" KeyboardNavigation.TabIndex="1" Panel.ZIndex="1"/>
                    <Border x:Name="ContentPanel" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" KeyboardNavigation.DirectionalNavigation="Contained" Grid.Row="1" KeyboardNavigation.TabIndex="2" KeyboardNavigation.TabNavigation="Local">
                        <ContentPresenter x:Name="PART_SelectedContentHost" ContentSource="SelectedContent" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style TargetType="{x:Type TabItem}">
    <Setter Property="Template" Value="{StaticResource TabItemTemplate}" />
</Style>

Don't know if this is exactly what you need, but you can add this data template and style to your Resource Dictionary to format an TabControl with a Flat Style...

    <ControlTemplate x:Key="TabItemTemplate" TargetType="{x:Type TabItem}">
    <Border Cursor="Hand" x:Name="tab" Background="White" BorderThickness="1,1,1,0" BorderBrush="Black" Padding="5">
        <Grid>
            <ContentPresenter   x:Name="contentPresenter" ContentSource="Header"
                                TextBlock.Foreground="Black"
                                VerticalAlignment="Center"
                                HorizontalAlignment="Left"/>
        </Grid>
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="IsSelected" Value="False">
            <Setter TargetName="tab" Property="Background" Value="Gray"/>
            <Setter TargetName="tab" Property="BorderBrush" Value="Black"/>
        </Trigger>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsSelected" Value="False" />
                <Condition Property="IsMouseOver" Value="True"/>
            </MultiTrigger.Conditions>
            <MultiTrigger.Setters>
                <Setter TargetName="tab" Property="BorderBrush" Value="White"/>
                <Setter TargetName="tab" Property="BorderThickness" Value="1,1,1,0"/>
            </MultiTrigger.Setters>
        </MultiTrigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

<Style TargetType="{x:Type TabControl}">
    <Setter Property="Background" Value="White"/>
    <Setter Property="BorderBrush" Value="Black"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabControl}">
                <Grid ClipToBounds="true" SnapsToDevicePixels="true" KeyboardNavigation.TabNavigation="Local">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <TabPanel x:Name="HeaderPanel" IsItemsHost="true" Margin="2,2,2,0" Grid.Row="0" KeyboardNavigation.TabIndex="1" Panel.ZIndex="1"/>
                    <Border x:Name="ContentPanel" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" KeyboardNavigation.DirectionalNavigation="Contained" Grid.Row="1" KeyboardNavigation.TabIndex="2" KeyboardNavigation.TabNavigation="Local">
                        <ContentPresenter x:Name="PART_SelectedContentHost" ContentSource="SelectedContent" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style TargetType="{x:Type TabItem}">
    <Setter Property="Template" Value="{StaticResource TabItemTemplate}" />
</Style>
浪漫之都 2024-12-25 01:14:42

由于您想让 TabItem 变得扁平,因此您需要的只是从 TabItem 的默认 ControlTemplate 中删除触发器,这些触发器负责为您提供TabItem 交互式控件的外观和感觉。尝试这个示例,希望这是您想要的 -

<Grid>
    <TabControl>
        <TabControl.Resources>
            <Style TargetType="TabItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="TabItem">
                            <Grid SnapsToDevicePixels="True">
                                <Border BorderThickness="1,1,1,0" Padding="{TemplateBinding Control.Padding}" BorderBrush="{TemplateBinding Border.BorderBrush}"
                                        Background="{TemplateBinding Panel.Background}" Name="Bd">
                                    <ContentPresenter RecognizesAccessKey="True" Content="{TemplateBinding HeaderedContentControl.Header}"
                                                      ContentTemplate="{TemplateBinding HeaderedContentControl.HeaderTemplate}"
                                                      ContentStringFormat="{TemplateBinding HeaderedContentControl.HeaderStringFormat}" 
                                                      ContentSource="Header" Name="Content" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                                                      SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                                </Border>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </TabControl.Resources>
        <TabItem Content="test1" Header="test1"/>
        <TabItem Content="test2" Header="test2"></TabItem>
        <TabItem Content="test3" Header="test3"></TabItem>
    </TabControl>
</Grid>

Since you want to make TabItems flat, all you need is to remove the triggers from the default ControlTemplate of TabItem which are responsible for giving your TabItem the look and feel of interactive control. Try this sample, hope this is what you want -

<Grid>
    <TabControl>
        <TabControl.Resources>
            <Style TargetType="TabItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="TabItem">
                            <Grid SnapsToDevicePixels="True">
                                <Border BorderThickness="1,1,1,0" Padding="{TemplateBinding Control.Padding}" BorderBrush="{TemplateBinding Border.BorderBrush}"
                                        Background="{TemplateBinding Panel.Background}" Name="Bd">
                                    <ContentPresenter RecognizesAccessKey="True" Content="{TemplateBinding HeaderedContentControl.Header}"
                                                      ContentTemplate="{TemplateBinding HeaderedContentControl.HeaderTemplate}"
                                                      ContentStringFormat="{TemplateBinding HeaderedContentControl.HeaderStringFormat}" 
                                                      ContentSource="Header" Name="Content" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                                                      SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                                </Border>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </TabControl.Resources>
        <TabItem Content="test1" Header="test1"/>
        <TabItem Content="test2" Header="test2"></TabItem>
        <TabItem Content="test3" Header="test3"></TabItem>
    </TabControl>
</Grid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文