WPF 标签样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17621883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:52:05  来源:igfitidea点击:

WPF label styling

wpfxamllabelcornerradius

提问by DoubleDunk

I have the following style:

我有以下风格:

<Style x:Key="WhiteStyle" TargetType="{x:Type Label}">               
    <Setter Property="BorderBrush" Value="White"/>
    <Setter Property="BorderThickness" Value="2"/>    
</Style>

However, I would like to add the property CornerRadiusand modify the value. Unfortunately, the XAML error says a Labeldoes not have a CornerRadiusproperty. My question, How must I modify this XAML?

但是,我想添加属性CornerRadius并修改值。不幸的是,XAML 错误说 aLabel没有CornerRadius属性。我的问题,我必须如何修改这个 XAML?

Thanks,

谢谢,

回答by TrueEddie

The error is correct, you cannot set a corner radius on a Label.

错误是正确的,您不能在标签上设置角半径。

What you can do is wrap the Label with a Border and apply your style to that to get the desired look.

您可以做的是用边框包裹标签并将您的样式应用于该样式以获得所需的外观。

EDIT:

编辑:

The Style Resource:

样式资源:

<Style x:Key="MyBorderStyle" TargetType="Border">
      <Setter Property="BorderBrush" Value="White" />
      <Setter Property="BorderThickness" Value="2" />
      <Setter Property="CornerRadius" Value="3" />
</Style>

The border wrapped label:

边框包裹标签:

<Border Style="{StaticResource MyBorderStyle}">
    <Label Content="My Label" />
</Border>