WPF标签设计

Bru*_*uie 8 wpf

我在WPF中有一个标签,我想重新设置,因此它有圆角.

我已经有以下代码:

<Style  TargetType="{x:Type Label}">        
    <Setter Property="Background" Value="Red"/>
    <Setter Property="Margin" Value="2,2,2,2"/>
    <Setter Property="BorderThickness" Value="2"/>
    <Setter Property="BorderBrush" Value="Blue"/>
   </Style>
Run Code Online (Sandbox Code Playgroud)

任何人都可以协助我如何在此标签上添加角半径

非常感谢

Ser*_*ioL 17

您需要更改Label的ControlTemplate才能获得圆角.Label控件本身不会公开CornerRadius属性.

将以下内容添加到样式中,您将在Label上获得圆角.我随意将其设置为"3",但你可以根据自己的需要设置它.

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type Label}">
            <Border BorderBrush="{TemplateBinding BorderBrush}" 
                BorderThickness="{TemplateBinding BorderThickness}" 
                Background="{TemplateBinding Background}" 
                Padding="{TemplateBinding Padding}" 
                SnapsToDevicePixels="true" 
                CornerRadius="3">
                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsEnabled" Value="false">
                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Setter.Value>
</Setter>
Run Code Online (Sandbox Code Playgroud)


Jag*_*uir 5

使用Border元素会更简单。

<Border CornerRadius="10" BorderThickness="2" BorderBrush="Blue" Background="Red" Margin="2">
    <Label Content="Lorem ipsum" />
</Border>
Run Code Online (Sandbox Code Playgroud)