WPF中的自定义形状边框

3
我正在尝试创建一个TabControl,以匹配我在WinForms中使用的现有TabControl的样式。其中一个功能是它具有类似于VS中找到的选项卡按钮形状(左侧倾斜,其他边缘是方形)。
我刚开始学习WPF,所以可能我走错了路。但是,我认为我需要获取一个Border,并将其附加到Path上,以便我可以为边框指定一些几何形状... 但是,我找不到任何有用的信息。我在S/O上找到了一个“FreeFormContentControl”类,但那是用于将内容遮罩成特定形状,而不是在特定形状周围绘制边框。
如果有人能指引我正确的方向,我将非常感激!
1个回答

2

您可以指定边框的属性BorderThickness和CornerRadius,以每个侧面的单独值为基础,例如:

<Border CornerRadius="2,2,0,0" BorderThickness="2,2,2,0"/>

它将把左上角和右上角的圆角设置为2,左、上和右边框部分设置为2。

更新:

您还可以创建自定义Adorner。更多信息请参见此MSDN文章

并且只需向TabItem控件模板添加一些自身大小的几何图形即可。有关此的更多信息,请参见此MSDN文章

示例

<Style TargetType="{x:Type TabItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabItem}">
                        <Grid>
                            <Viewbox Width="{TemplateBinding Width}"  Height="{TemplateBinding Height}" Stretch="Fill" StretchDirection="DownOnly">
                                <Path x:Name="path" Stretch="Fill" Stroke="Black" Fill="{StaticResource LightBrush}" Width="Auto" Height="Auto" Data="M 0,20 L 0,5 5,0 100,0 100,20 "/>
                            </Viewbox>

                            <Border Visibility="Visible"
            x:Name="Border"                                                                  
            Margin="5,1,0,1" >
                                <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 Property="Panel.ZIndex" Value="100" />
                                <Setter TargetName="path" Property="Fill" Value="{StaticResource WindowBackgroundBrush}" />

                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter TargetName="path" Property="Fill" Value="{StaticResource DisabledBackgroundBrush}" />
                                <Setter TargetName="path" Property="Stroke" Value="{StaticResource DisabledBorderBrush}" />
                                <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

但这会给我一个带有圆角的矩形。VS中的选项卡形状是左侧倾斜,而不是直边... - Matt Whitfield
仍然不是我正在寻找的 - Adorner 不会改变 Border 的基本布局,第二篇文章中也没有任何提示如何将 Geometry 附加到 TabItem 模板? - Matt Whitfield
请注意ViewBox和其中的Path。您可以自行定义使用的颜色。 - Eugene Cheverda

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接