WPF标签样式设计

6

I have the following style:

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

然而,我想要添加属性CornerRadius并修改其值。不幸的是,XAML错误显示Label没有CornerRadius属性。我的问题是,我该如何修改这个XAML?

谢谢。


您需要将一个依赖属性附加到现有的控件:http://stackoverflow.com/questions/14318707/add-dependency-property-to-existing-net-class。我建议将此样式应用于`TargetType="{x:Type Border}"`,并在标签周围包装边框。 - Dom
1个回答

13

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

您可以使用边框包装标签,并将样式应用于边框以获得所需的外观。

编辑:

样式资源:

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

边框包裹的标签:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接