StrikeThrough effect on a TextBlock

2020-04-06 01:33发布

问题:

In a Windows Store App, is there any way to apply a StrikeThrough effect on a TextBlock's content? If there isn't, a RichTextBlock or any similar controls would do. It doesn't matter if it's through XAML or programmatically (C#), but I'd prefer through XAML so it would show in the designer.

I found this among Microsoft's documentation, but I don't know how to use it: http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.text.itextcharacterformat.strikethrough.aspx

回答1:

Windows Store apps have their own control namespace (System.Ui.Xaml.Controls). You'll see that the TextBlock for Windows Store apps doesn't have a TextDecoration property: TextBlock documentation.

The "full" .Net 4.5 TextBlock does have a TextDecoration property:.Net 4.5 WPF TextBlock Documentation.

You can do this in a "hacky" way like so:

    <Grid Height="30">
        <TextBlock Text="This is a test" FontSize="22" Height="34" HorizontalAlignment="Center" Foreground="White"   />
        <Line Stretch="Fill" Stroke="white" StrokeThickness="2 " X1="1" Width="120" VerticalAlignment="Center"  HorizontalAlignment="Center"   />
    </Grid>


回答2:

It seems it isn't possible after all:

"we will not able to include the TextDecoration class for the final realese of the XAML framework for Windows 8. This means that you will not be able to use the strikethrough feature, but you will still be able to underline text via the Underline class."

(http://social.msdn.microsoft.com/Forums/en-CA/winappswithcsharp/thread/cba0c363-60da-4e4e-9fc1-93f7c3658aff)

However, Microsoft is working on this:

"This is a known limitation in XAML text story in the Windows 8 developer preview. On the bright side this is an area with planned improvements happening soon hereafter and we expect to resolve this issue."

(http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/d5362fe2-48f6-448d-92ae-25216345c46d)