原文:WPF编程,TextBlock中的文字修饰线(上划线,中划线,基线与下划线)的使用方法。
版权声明:我不生产代码,我只是代码的搬运工。 https://blog.****.net/qq_43307934/article/details/88962940
参考地址:https://blog.****.net/johnsuna/article/details/1895169
效果:
从上至下,分别为上划线(Overline),中划线(StrikeThrough),基线(Baseline),下划线(Underline)。
1、 简单设置
<TextBlock TextDecorations="Strikethrough" Text="文字"/>
C#后台设置:
textBlock1.TextDecorations = TextDecorations.Underline;
textBlock1为控件TextBlock的名称
2、调整下划线与文字的距离
<TextBlock FontSize="84"
Text="文字">
<TextBlock.TextDecorations>
<TextDecorationCollection>
<TextDecoration PenOffset="3" />
</TextDecorationCollection>
</TextBlock.TextDecorations>
</TextBlock>
3、为文字加双下划线
<TextBlock FontSize="84"
Text="文字">
<TextBlock.TextDecorations>
<TextDecorationCollection>
<TextDecoration PenOffset="1" />
<TextDecoration PenOffset="3" />
</TextDecorationCollection>
</TextBlock.TextDecorations>
</TextBlock>
4、下划线的颜色与厚度
<TextBlock FontSize="84"
Text="文字">
<TextBlock.TextDecorations>
<TextDecoration PenOffset="10"
PenOffsetUnit="Pixel"
PenThicknessUnit="Pixel">
<TextDecoration.Pen>
<Pen Thickness="5"
Brush="Green" />
</TextDecoration.Pen>
</TextDecoration>
</TextBlock.TextDecorations>
</TextBlock>
5、下划线的渐变色
<TextBlock FontSize="84"
Text="文字">
<TextBlock.TextDecorations>
<TextDecoration PenOffset="10"
PenOffsetUnit="Pixel"
PenThicknessUnit="Pixel">
<TextDecoration.Pen>
<Pen Thickness="5">
<Pen.Brush>
<LinearGradientBrush Opacity="0.8"
StartPoint="0,0.5"
EndPoint="1,0.5">
<LinearGradientBrush.GradientStops>
<GradientStop Color="Yellow"
Offset="0" />
<GradientStop Color="Red"
Offset="1" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Pen.Brush>
<Pen.DashStyle>
<DashStyle Dashes="1,2,3" />
</Pen.DashStyle>
</Pen>
</TextDecoration.Pen>
</TextDecoration>
</TextBlock.TextDecorations>
</TextBlock>
C#后台实现:(此部分作者:大可山人)
TextDecoration myUnderline = new TextDecoration();
Pen myPen = new Pen();
myPen.Brush = new LinearGradientBrush(Colors.Yellow, Colors.Red, new Point(0, 0.5), new Point(1, 0.5));
myPen.Brush.Opacity = 0.8;
myPen.Thickness = 5;
myPen.DashStyle = DashStyles.Dash;
myUnderline.Pen = myPen;
myUnderline.PenThicknessUnit = TextDecorationUnit.FontRecommended;
TextDecorationCollection myCollection = new TextDecorationCollection();
myCollection.Add(myUnderline);
textBlockGood.TextDecorations = myCollection;