我正在以这种方式改变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
前景和透视线有不同的颜色。
答案 0 :(得分:19)
我认为这就是你要找的东西。
<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>
</TextBlock >
答案 1 :(得分:2)
您遇到的问题是您在文本上覆盖了一行。当文本换行时,你需要创建另一行并不容易。
你可以通过不使用该行来解决这个问题,而是使用特定的笔来代替后面代码中删除线的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