【问题标题】:WPF Label DesignWPF 标签设计
【发布时间】:2011-03-09 04:28:16
【问题描述】:

我在 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>

谁能帮助我如何在这个标签上添加一个角半径

非常感谢

【问题讨论】:

    标签: wpf


    【解决方案1】:

    您需要更改标签的 ControlTemplate 以获得圆角。 Label 控件本身不公开 CornerRadius 属性。

    将以下内容添加到您的样式中,您将在标签上获得圆形边缘。我在下面任意将其设置为“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>
    

    【讨论】:

      【解决方案2】:

      使用Border 元素会更简单。

      <Border CornerRadius="10" BorderThickness="2" BorderBrush="Blue" Background="Red" Margin="2">
          <Label Content="Lorem ipsum" />
      </Border>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-29
        • 1970-01-01
        • 2013-10-21
        • 2012-10-30
        • 2012-05-12
        • 2011-10-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多