如何在WPF选项卡控件中创建梯形选项卡

2023-12-20

如何在WPF选项卡控件中创建梯形选项卡?
我想创建非矩形选项卡,看起来像 Google Chrome 中的选项卡或 VS 2008 代码编辑器中的选项卡。

可以用 WPF 样式完成还是必须用代码绘制?

互联网上有可用的代码示例吗?

Edit:

There is lots of examples that show how to round corners or change colors of tabs, but I could not find any that changes geometry of tab like this two examples:

VS 2008 Code Editor Tabs
VS 2008 Code Editor Tabs


Google Chrome tabs
alt text

这两个示例中的选项卡不是矩形,而是梯形。


我试图在互联网上找到一些针对这个问题的控制模板或解决方案,但我没有找到任何适合我的“可接受”的解决方案。所以我用我的方式写了它,这是我第一次(也是最后一次=))尝试这样做的一个例子:

<Window x:Class="TabControlTemplate.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="clr-namespace:TabControlTemplate"
    Title="Window1" Width="600" Height="400">
<Window.Background>
    <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
        <GradientStop Color="#FF3164a5" Offset="1"/>
        <GradientStop Color="#FF8AAED4" Offset="0"/>
    </LinearGradientBrush>
</Window.Background>
<Window.Resources>
    <src:ContentToPathConverter x:Key="content2PathConverter"/>
    <src:ContentToMarginConverter x:Key="content2MarginConverter"/>

    <SolidColorBrush x:Key="BorderBrush" Color="#FFFFFFFF"/>
    <SolidColorBrush x:Key="HoverBrush" Color="#FFFF4500"/>
    <LinearGradientBrush x:Key="TabControlBackgroundBrush" EndPoint="0.5,0" StartPoint="0.5,1">
        <GradientStop Color="#FFa9cde7" Offset="0"/>
        <GradientStop Color="#FFe7f4fc" Offset="0.3"/>
        <GradientStop Color="#FFf2fafd" Offset="0.85"/>
        <GradientStop Color="#FFe4f6fa" Offset="1"/>
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="TabItemPathBrush" StartPoint="0,0" EndPoint="0,1">
        <GradientStop Color="#FF3164a5" Offset="0"/>
        <GradientStop Color="#FFe4f6fa" Offset="1"/>
    </LinearGradientBrush>

    <!-- TabControl style -->
    <Style x:Key="TabControlStyle" TargetType="{x:Type TabControl}">
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="TabControl">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <Border Grid.Row="1" BorderThickness="2,0,2,2" Panel.ZIndex="2" CornerRadius="0,0,2,2"
                                BorderBrush="{StaticResource BorderBrush}"
                                Background="{StaticResource TabControlBackgroundBrush}">
                            <ContentPresenter ContentSource="SelectedContent"/>
                        </Border>
                        <StackPanel Orientation="Horizontal" Grid.Row="0" Panel.ZIndex="1" IsItemsHost="true"/>
                        <Rectangle Grid.Row="0" Height="2" VerticalAlignment="Bottom"
                                   Fill="{StaticResource BorderBrush}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <!-- TabItem style -->
    <Style x:Key="{x:Type TabItem}" TargetType="{x:Type TabItem}">
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="TabItem">
                    <Grid x:Name="grd">
                        <Path x:Name="TabPath" StrokeThickness="2"
                              Margin="{Binding ElementName=TabItemContent, Converter={StaticResource content2MarginConverter}}"
                              Stroke="{StaticResource BorderBrush}"
                              Fill="{StaticResource TabItemPathBrush}">
                            <Path.Data>
                                <PathGeometry>
                                    <PathFigure IsClosed="False" StartPoint="1,0" 
                                                Segments="{Binding ElementName=TabItemContent, Converter={StaticResource content2PathConverter}}">
                                    </PathFigure>
                                </PathGeometry>
                            </Path.Data>
                            <Path.LayoutTransform>
                                <ScaleTransform ScaleY="-1"/>
                            </Path.LayoutTransform>
                        </Path>
                        <Rectangle x:Name="TabItemTopBorder" Height="2" Visibility="Visible"
                                   VerticalAlignment="Bottom" Fill="{StaticResource BorderBrush}"
                                   Margin="{Binding ElementName=TabItemContent, Converter={StaticResource content2MarginConverter}}" />
                        <ContentPresenter x:Name="TabItemContent" ContentSource="Header"
                                          Margin="10,2,10,2" VerticalAlignment="Center"
                                          TextElement.Foreground="#FF000000"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True" SourceName="grd">
                            <Setter Property="Stroke" Value="{StaticResource HoverBrush}" TargetName="TabPath"/>
                        </Trigger>
                        <Trigger Property="Selector.IsSelected" Value="True">
                            <Setter Property="Fill" TargetName="TabPath">
                                <Setter.Value>
                                    <SolidColorBrush Color="#FFe4f6fa"/>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="BitmapEffect">
                                <Setter.Value>
                                    <DropShadowBitmapEffect Direction="302" Opacity="0.4" 
                                                        ShadowDepth="2" Softness="0.5"/>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Panel.ZIndex" Value="2"/>
                            <Setter Property="Visibility" Value="Hidden" TargetName="TabItemTopBorder"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid Margin="20">
    <TabControl Grid.Row="0" Grid.Column="1" Margin="5" TabStripPlacement="Top" 
                Style="{StaticResource TabControlStyle}" FontSize="16">
        <TabItem Header="MainTab">
            <Border Margin="10">
                <TextBlock Text="The quick brown fox jumps over the lazy dog."/>
            </Border>
        </TabItem>
        <TabItem Header="VeryVeryLongTab" />
        <TabItem Header="Tab" />
    </TabControl>
</Grid>
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace TabControlTemplate
{
public partial class Window1
{
    public Window1()
    {
        InitializeComponent();
    }
}

public class ContentToMarginConverter: IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return new Thickness(0, 0, -((ContentPresenter)value).ActualHeight, 0);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

public class ContentToPathConverter: IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var ps = new PathSegmentCollection(4);
        ContentPresenter cp = (ContentPresenter)value;
        double h = cp.ActualHeight > 10 ? 1.4 * cp.ActualHeight : 10;
        double w = cp.ActualWidth > 10 ? 1.25 * cp.ActualWidth : 10;
        ps.Add(new LineSegment(new Point(1, 0.7 * h), true));
        ps.Add(new BezierSegment(new Point(1, 0.9 * h), new Point(0.1 * h, h), new Point(0.3 * h, h), true));
        ps.Add(new LineSegment(new Point(w, h), true));
        ps.Add(new BezierSegment(new Point(w + 0.6 * h, h), new Point(w + h, 0), new Point(w + h * 1.3, 0), true));
        return ps;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}
}

我编写的这两个转换器是为了根据其内容调整选项卡大小。实际上,我根据内容大小制作 Path 对象。如果您不需要具有各种宽度的选项卡,您可以使用一些修改后的副本:

<Style x:Key="tabPath" TargetType="{x:Type Path}">
      <Setter Property="Stroke" Value="Black"/>
      <Setter Property="Data">
          <Setter.Value>
              <PathGeometry Figures="M 0,0 L 0,14 C 0,18 2,20 6,20 L 60,20 C 70,20 80,0 84,0"/>
          </Setter.Value>
      </Setter>
  </Style>

screen:

示例项目(vs2010) http://bitbucket.org/rooks/trapezoidtabs

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在WPF选项卡控件中创建梯形选项卡 的相关文章

  • DataContractSerializer 事件/委托字段问题

    在我的 WPF 应用程序中 我正在使用DataContractSerializer序列化对象 我发现它无法序列化具有事件或委托声明的类型 考虑以下失败的代码 Serializable public abstract class BaseCl
  • 是否可以“缩放”WPF RichTextBox 中的文本?

    我注意到 WinForms RichTextBox 有一个ZoomFactor我认为这正是我想要的属性 不幸的是 WPF 变体似乎完全缺少此属性 有什么方法可以实现相同的功能 增加 减少整个文档的可见文本大小而不实际更改底层 RTF Upd
  • Android 在 WPF 中的涟漪效应

    我喜欢 Android 的新动画 当你触摸一个控件 listviewitem 按钮等 时 它会执行如下所示的简洁动画 我想知道如何在全球范围内为 WPF 中的所有 可点击 控件以良好的方式实现这一点 我特别需要帮助的是如何在控件上创建圆圈
  • WPF 列表框 + 扩展器事件

    我在列表框的 ItemTemplate 中有一个 Expander 渲染得很好 我遇到的问题是 我希望在展开和 或选择扩展器时触发 ListBox SelectionChanged 事件 MouseDown 事件似乎没有冒泡到 ListBo
  • WPF Dispatchertimer 延迟反应/冻结

    在我的 WPF 应用程序中 我使用 3 个不同的 DispatcherTimers 一种是用于显示当前时间 一种是每 5 秒运行一次数据库查询 第三个每 1 秒刷新一次自定义按钮的值 当我的程序运行时 有很多延迟 冻结 例如 时间开始正确计
  • 以编程方式获取 DataGridColumnHeader ContextMenu

    我在 View cs 中有这段代码 var contextMenu this dataGridFacade GiveContextMenuForDataGrid this DataGridAllJobs this DataGridAllJo
  • 支持全方向动画的扩展器

    我的目标 是创建一个带有扩展动画的自定义扩展器 并且应该支持所有方向 我尝试过的 我在以下人员的帮助下实施了一个解决方案this http www codeproject com Articles 248112 Templating WPF
  • 使用自定义视觉效果创建无边框窗户

    我想使用 WPF 创建一个看起来不像常规窗口的自定义窗口 我想要创建的窗口类似于MacOs窗口 标题居中 带有阴影效果 控件按钮位于窗口左侧 我确实创建了一个无边框窗口 但我有一些问题 1 如何在 XAML 中设置最大化 最小化和关闭按钮的
  • Visual Studio 2010 解决方案的 MSBuild 命令行执行无法引用 VS2010 SDK 来构建扩展

    Scenario 包含 VS2010 扩展的解决方案 NET 4 类库 带有 WPF UI 它在视觉工作室内部构建得很好 On the 同一台机器当我尝试通过命令行构建它时 作为构建脚本的一部分 Fails when using this
  • 从单元测试调用时,Application.Current 为 null

    我有一个尝试从单元测试中调用的方法 该方法在现实生活中将从后台线程运行 它使用一些代码来启动 UI 线程的调用更新 使用Application Current Dispatcher BeginInvoke However Applicati
  • 如何在WPF中裁剪图像并保存到ImageSource中?

    我是 WPF 的新学员 我有一个问题 我有一个图像 宽度 360 高度 360 在这里 我想裁剪该图像 如下所示 0 0 到 120 120 保存到第一个 ImageSource 对象 120 0 到 240 120 保存到第二个 Imag
  • 底部垂直滚动richtextbox [WPF]

    我有一个富文本框 当满了时我想自动滚动到底部 这可能吗 用xaml做这个吗 通过使用ScrollViewer ScrollChanged http msdn microsoft com en us library system window
  • 在单个 WPF 控件中列出所有 Validation.Error?

    我试图找到一种简单的方法来绑定单个控件 例如 TextBlock 或 ListBox 以列出 WPF 表单上的所有验证错误 我能找到的大多数源代码示例只是将一个控件绑定到 Validation Errors 0 ErrorContent 它
  • 如何在 Windows Phone 8 中使用 ProgressRing

    在参考中http briandunnington github io progressring wp8 html http briandunnington github io progressring wp8 html为了实现一个有趣的进度
  • 如何在 WPF 数据网格中添加页脚行?

    如何在 WPF 数据网格中添加页脚行 我必须在 WPF 数据网格中为每列的总和添加一行 我不想使用任何 dll 或 telerik 以及类似的东西 仅使用 Microsoft 组件来执行此操作 我正在尝试这样做
  • WPF DataGrid 绑定到 string.Length 而不是字符串文本

    我是 WPF 的新手 并且真诚地尝试自己尽可能多地弄清楚 我已经创建了第一个 DataGrid 控件 并且尝试使用如下字符串列表填充它
  • WPF 应用程序中的调度程序实现多个异步任务

    在下面的 MSDN 示例中WPF应用程序 它演示了async await多异步的实现Tasks the Dispatcher对象显然没有被使用 需要 即异步执行Tasks似乎可以直接访问 UI 控件 在本例中resultTextBox Te
  • 将 SQL 数据库附加到 ComboBox.ItemSsource (WPF)

    我想知道如何将 SQL Server 数据库分配给 ComboBox 的 ItemSource 属性 在 WPF 应用程序中 我将数据源分配给项目 但不知道如何分配给属性 此致 你可以这样尝试 你可以像下面这样绑定组合框的项目源属性 Ite
  • WPF (MVVM) 菜单中的互斥(且可绑定)复选框

    我试图找到一个在 WPF MVVM 应用程序的菜单中使用复选框的示例 该应用程序可以绑定到底层 ViewModel 类中的枚举 我有一个简单的例子 public class MyViewModel public MyViewModel co
  • 如何确定我的 TextBlock 文本是否正在被修剪?

    以下文本块按预期换行和修剪 修剪文本时会显示省略号

随机推荐