2010-06-28 10 views
7

Ho un'etichetta in WPF che desidero ridimensionare in modo che abbia gli angoli arrotondati.Design etichetta WPF

Ho il codice qui sotto già:

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

Qualcuno può aiutare con come vorrei aggiungere un raggio d'angolo a questa etichetta

molte grazie

risposta

15

Avrai bisogno di cambiare il ControlTemplate per l'etichetta per ottenere angoli arrotondati. Il controllo Label non espone una proprietà CornerRadius.

Aggiungi quanto segue al tuo Stile e otterrai bordi arrotondati sull'etichetta. L'ho impostato arbitrariamente su "3" di seguito, ma puoi impostarlo in base alle tue esigenze.

<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

Eccellente, grazie. – Bruie

3

Utilizzare l'elemento Border sarebbe più semplice.

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