如何更改 WPF TextBlock 中的 TextDecoration 颜色?
How to change TextDecoration color in WPF TextBlock?
我正在以这种方式更改 TextDecoration
的颜色:
<Grid Background="{x:Null}"
Margin="10,0,10,0">
<TextBlock Text="{Binding Value}"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Style="{StaticResource SWMRegularTextBlockStyle}"
Margin="0"
FontSize="{DynamicResource RegularFontSize}"
x:Name="tb" />
<Line VerticalAlignment="Center"
HorizontalAlignment="Center"
Visibility="{Binding InStock, Converter={StaticResource ReverseBooleanToVisiblity}}"
Stroke="Red"
Margin="0"
StrokeThickness="2"
X1="1"
Stretch="Fill"
Width="{Binding ActualWidth, ElementName=tb, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
但是当 Text
有两行时它失败了。请帮我更改 TextDecoration 的颜色。
提前致谢。
注意:我想要 TextBlock
不同颜色的前景和删除线。
您遇到的问题是您在文本上覆盖了一条线。当文本换行时,您需要创建另一行,这并不容易。
您可以通过根本不使用线条来解决这个问题,而是在后面的代码中为删除线的 TextDecoration 使用特定的笔。
private void WindowLoaded(object sender, EventArgs e)
{
// Fill the overline decoration with a solid color brush.
TextDecorationCollection myCollection = new TextDecorationCollection();
TextDecoration myStrikeThrough = new TextDecoration();
myStrikeThrough.Location = TextDecorationLocation.Strikethrough;
// Set the solid color brush.
myStrikeThrough.Pen = new Pen(Brushes.Red, 2);
myStrikeThrough.PenThicknessUnit = TextDecorationUnit.FontRecommended;
// Set the underline decoration to the text block.
myCollection.Add(myStrikeThrough);
tb.TextDecorations = myCollection;
}
然后简化您的 XAML。删除 Line 控件,并将 Loaded="WindowLoaded"
添加到您的 Window
我想这就是你要找的。
<TextBlock Text="{Binding Value}" VerticalAlignment="Center" HorizontalAlignment="Center" Style="{StaticResource SWMRegularTextBlockStyle}" Margin="0" FontSize="{DynamicResource RegularFontSize}" x:Name="tb" >
<TextBlock.TextDecorations>
<TextDecoration Location="Strikethrough">
<TextDecoration.Pen>
<Pen Brush="Red" />
</TextDecoration.Pen>
</TextDecoration>
</TextBlock.TextDecorations>
</TextBlock>
我正在以这种方式更改 TextDecoration
的颜色:
<Grid Background="{x:Null}"
Margin="10,0,10,0">
<TextBlock Text="{Binding Value}"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Style="{StaticResource SWMRegularTextBlockStyle}"
Margin="0"
FontSize="{DynamicResource RegularFontSize}"
x:Name="tb" />
<Line VerticalAlignment="Center"
HorizontalAlignment="Center"
Visibility="{Binding InStock, Converter={StaticResource ReverseBooleanToVisiblity}}"
Stroke="Red"
Margin="0"
StrokeThickness="2"
X1="1"
Stretch="Fill"
Width="{Binding ActualWidth, ElementName=tb, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
但是当 Text
有两行时它失败了。请帮我更改 TextDecoration 的颜色。
提前致谢。
注意:我想要 TextBlock
不同颜色的前景和删除线。
您遇到的问题是您在文本上覆盖了一条线。当文本换行时,您需要创建另一行,这并不容易。
您可以通过根本不使用线条来解决这个问题,而是在后面的代码中为删除线的 TextDecoration 使用特定的笔。
private void WindowLoaded(object sender, EventArgs e)
{
// Fill the overline decoration with a solid color brush.
TextDecorationCollection myCollection = new TextDecorationCollection();
TextDecoration myStrikeThrough = new TextDecoration();
myStrikeThrough.Location = TextDecorationLocation.Strikethrough;
// Set the solid color brush.
myStrikeThrough.Pen = new Pen(Brushes.Red, 2);
myStrikeThrough.PenThicknessUnit = TextDecorationUnit.FontRecommended;
// Set the underline decoration to the text block.
myCollection.Add(myStrikeThrough);
tb.TextDecorations = myCollection;
}
然后简化您的 XAML。删除 Line 控件,并将 Loaded="WindowLoaded"
添加到您的 Window
我想这就是你要找的。
<TextBlock Text="{Binding Value}" VerticalAlignment="Center" HorizontalAlignment="Center" Style="{StaticResource SWMRegularTextBlockStyle}" Margin="0" FontSize="{DynamicResource RegularFontSize}" x:Name="tb" >
<TextBlock.TextDecorations>
<TextDecoration Location="Strikethrough">
<TextDecoration.Pen>
<Pen Brush="Red" />
</TextDecoration.Pen>
</TextDecoration>
</TextBlock.TextDecorations>
</TextBlock>