TabControl 的自定义 ControlTemplate

发布于 2024-12-08 11:43:50 字数 597 浏览 1 评论 0原文

目前,在我的一个视图中,我有以下 TabControl 设置:

    <TabControl ItemsSource="{Binding Workspaces}"
                ItemTemplate="{StaticResource WorkspaceTemplate}"
                IsSynchronizedWithCurrentItem="True" Background="{x:Null}">

我想通过设置 ControlTemplate 属性来完全更改 TabControl 的外观和感觉。我的计划是不让任何其他选项卡可见,只让当前选项卡的内容可见,在任何地方都看不到 TabControl,但仍然允许用户通过 Ctrl+Tab 切换到其他选项卡。

坦白说,我不知道从哪里开始。我尝试将 ContentPresenter 添加到 TabControl 的 ControlTemplate,但我不知道如何将其绑定到当前活动的选项卡(注意,不是实际的选项卡,而是 TabItem 的内容)。

有什么帮助或者一些样板代码可以帮助我开始吗?也许有一个更合适的控件(即使这意味着我将不得不失去 Ctrl+Tab 功能)。

Currently, in my one of my Views, I have the following TabControl setup:

    <TabControl ItemsSource="{Binding Workspaces}"
                ItemTemplate="{StaticResource WorkspaceTemplate}"
                IsSynchronizedWithCurrentItem="True" Background="{x:Null}">

I would like to change the look and feel of the TabControl completely by setting up the ControlTemplate property. My plan is to have none of the other tabs visible and only have the contents of the current tab visible with no trace of the TabControl anywhere but still allowing user to Ctrl+Tab to other tabs.

Frankly speaking, I have no idea where to start. I tried adding a ContentPresenter to the TabControl's ControlTemplate but I can't figure out how to bind it to the currently active tab (note, not the actual tab but the TabItem's contents).

Any help or perhaps some boilerplate code to get me started? Perhaps there's a more suitable control out there (even if it means I'll have to lose the Ctrl+Tab functionality).

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

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

发布评论

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

评论(1

山田美奈子 2024-12-15 11:43:50

您可以创建自己的 ControlTemplate。

<ControlTemplate TargetType="{x:Type TabItem}">
<Grid>
    <Border 
            Name="Border"
            Margin="0,0,-4,0" 
            Background="LightGray"
            BorderBrush="Black" 
            BorderThickness="1,1,1,1"
            Visibility="Collapsed"
            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>
    <Trigger Property="IsSelected" Value="True">
        <Setter TargetName="Border" Property="Background" Value="Gray" />
        <Setter TargetName="Border" Property="BorderThickness" Value="1,1,1,0" />
        <Setter TargetName="Border" Property="Visibility" Value="Visible" />
    </Trigger>
</ControlTemplate.Triggers>

You can just create your own ControlTemplate.

<ControlTemplate TargetType="{x:Type TabItem}">
<Grid>
    <Border 
            Name="Border"
            Margin="0,0,-4,0" 
            Background="LightGray"
            BorderBrush="Black" 
            BorderThickness="1,1,1,1"
            Visibility="Collapsed"
            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>
    <Trigger Property="IsSelected" Value="True">
        <Setter TargetName="Border" Property="Background" Value="Gray" />
        <Setter TargetName="Border" Property="BorderThickness" Value="1,1,1,0" />
        <Setter TargetName="Border" Property="Visibility" Value="Visible" />
    </Trigger>
</ControlTemplate.Triggers>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文