2010-06-28 1107 views
7

我在WPF中有一个标签,我想restyle因此它有圆角。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> 

谁能请帮助我如何将一个角半径添加到该标签

千恩万谢

回答

15

你需要改变控制标签的模板以获得圆角。 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> 
+0

非常好 - 谢谢 – Bruie 2010-06-29 07:44:16

3

使用Border元素会更简单。

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