How to create trapezoid tabs in WPF tab control

前端 未结 7 1870
借酒劲吻你
借酒劲吻你 2020-11-29 15:43

How to create trapezoid tabs in WPF tab control?
I\'d like to create non rectangular tabs that look like tabs in Google Chrome or like tabs in code editor of VS 2008.

相关标签:
7条回答
  • 2020-11-29 16:12

    To slope both left and right tab edges, here is a modification of Slauma's enhancement to rook's accepted answer. This is a replacement of Convert method of the ContentToPathConverter class:

        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;
            // Smaller unit, so don't need fractional multipliers.
            double u = 0.1 * h;
            // HACK: Start before "normal" start of tab.
            double x0 = -4 * u;
            // end of transition
            double x9 = w + 8 * u;
            // transition width
            double tw = 8 * u;
            // top "radius" (actually, gradualness of curve. Larger value is more rounded.)
            double rt = 5 * u;
            // bottom "radius" (actually, gradualness of curve. Larger value is more rounded.)
            double rb = 3 * u;
            // "(x0, 0)" is start point - defined in PathFigure.
            // Cubic: From previous endpoint, 2 control points + new endpoint.
            ps.Add(new BezierSegment(new Point(x0 + rb, 0), new Point(x0 + tw - rt, h), new Point(x0 + tw, h), true));
            ps.Add(new LineSegment(new Point(x9 - tw, h), true));
            ps.Add(new BezierSegment(new Point(x9 - tw + rt, h), new Point(x9 - rb, 0), new Point(x9, 0), true));
    
            // "(x0, 0)" is start point.
            PathFigure figure = new PathFigure(new Point(x0, 0), ps, false);
            PathGeometry geometry = new PathGeometry();
            geometry.Figures.Add(figure);
    
            return geometry;
        }
    

    Also in TabControl's ControlTemplate, add left (and optionally right) margins to the container of the tab items (the only change is added Margin="20,0,20,0"):

    <Style x:Key="TabControlStyle" TargetType="{x:Type TabControl}">
        ...
        <Setter Property="Template">
            ...
            <StackPanel Grid.Row="0" Panel.ZIndex="1" Orientation="Horizontal" IsItemsHost="true" Margin="20,0,20,0"/>
    

    Issue: There is a slight visual "glitch" at bottom of left edge of tab, when it is not the selected tab. I think it is related to "going backwards" to start before beginning of tab area. Or else related to drawing of line line at bottom of tab (which doesn't know we start before the left edge of the tabs "normal" rectangle).

    0 讨论(0)
提交回复
热议问题