如何在WPF中制作Label
文本Underline
?我被困了,找不到下划线的任何财产:
<Label Name="lblUserName"
Content="Username"
FontSize="14" FontWeight="Medium" />
在Label
没有TextDecorations
,因此试试这个:
<Label Width="100" Height="30">
<TextBlock TextDecorations="Underline">TestText</TextBlock>
</Label>
Edit: more universal solution
在这种情况下,使用Label.Content
代替Label.Tag
,因为Content属性只能设置一次:
<Label Tag="TestContent"
Width="100"
Height="30"
HorizontalContentAlignment="Center"
Background="AliceBlue">
<TextBlock TextDecorations="Underline"
Text="{Binding Path=Tag,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type Label}}}" />
</Label>
这是风格的答案。
内容:
<Label>
<TextBlock Style="{DynamicResource StyleName}">text content</TextBlock>
</Label>
风格:
<Style x:Key="StyleName">
<Setter Property="TextBlock.TextDecorations" Value="Underline" />
<Setter Property="TextBlock.FontStyle" Value="Italic" />
</Style>
这是将样式直接应用于Label的方法:
<Style TargetType="Label">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding}" TextDecorations="Underline"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
这简化了标签项:
<Label>
Label 1
</Label>
<Label Grid.Row="1">
Label 2
</Label>
如果标签的内容仅为文本,则此方法有效。