Windows 10 XAML Tips: AnimatedTextBlock

By Fons Sonnemans, posted on
4427 Views

For one of my Windows games I created an AnimatedTextBlock control which animates the Text when it changes. This attracks the user attention. In this blog I will explain how I implemented it. The following animated GIF show's you this AnimtatedTextBlock in my sample app. Everytime you tap the button the text is changed from 'Hello' to 'World' and back.

AnimatedTextBlock

The AnimatedTextBlock is an UserControl which contains a TextBlock and a Storyboard. The Storyboard contains a PlaneProjection.RotationX animation which rotates the TextBlock and a TextBlock.Text animation which sets the Text. The Font properties of the TextBlock like FontSize and FontFamily are inherited from the UserControl.

<UserControl x:Class="App91.Views.Controls.AnimatedTextBlock"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             d:DesignHeight="22.667"
             d:DesignWidth="400">
    <UserControl.Resources>
        <Storyboard x:Name="Storyboard1">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(PlaneProjection.RotationX)"
                                           Storyboard.TargetName="textBlockProjection">
                <EasingDoubleKeyFrame KeyTime="0"
                                      Value="0" />
                <EasingDoubleKeyFrame KeyTime="0:0:0.2"
                                      Value="90" />
                <EasingDoubleKeyFrame KeyTime="0:0:0.4"
                                      Value="0" />
            </DoubleAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Text)"
                                           Storyboard.TargetName="innerTextBlock">
                <DiscreteObjectKeyFrame KeyTime="0:0:0.2"
                                        x:Name="textFrame" />
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>
    </UserControl.Resources>
    <TextBlock x:Name="innerTextBlock"
               TextWrapping="{x:Bind TextWrapping, Mode=OneWay}"
               RenderTransformOrigin="0.5,0.5">
        <TextBlock.Projection>
            <PlaneProjection RotationX="0"
                             x:Name="textBlockProjection" />
        </TextBlock.Projection>
    </TextBlock>
</UserControl>

In the code behind of the UserControl there is a Text dependency property which will allow Styling and DataBinding of this property. The OnTextPropertyChanged method is automatically called when the value of this property changes. In this method the value of the Text animation is set to the new value and the Storyboard is started. The class also contains a TextWrapping dependency property which is used in the XAML code to databind the TextBlock TextWrapping property to using compiled binding. For my solution I only needed this TextWrapping property but you might have to add extra properties like TextAlignment or TextLineBounds.

public sealed partial class AnimatedTextBlock : UserControl {

    public AnimatedTextBlock() {
        this.InitializeComponent();
    }

    public string Text {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register(nameof(Text), typeof(string),
            typeof(AnimatedTextBlock),
            new PropertyMetadata(null, OnTextPropertyChanged));

    private static void OnTextPropertyChanged(DependencyObject d,
                            DependencyPropertyChangedEventArgs e) {
        var source = d as AnimatedTextBlock;
        if (source != null) {
            var value = (string)e.NewValue;
            if (e.OldValue != null) {
                source.textFrame.Value = value;
                source.Storyboard1.Begin();
            } else {
                source.innerTextBlock.Text = value;
            }
        }
    }

    public TextWrapping TextWrapping {
        get { return (TextWrapping)GetValue(TextWrappingProperty); }
        set { SetValue(TextWrappingProperty, value); }
    }

    public static readonly DependencyProperty TextWrappingProperty =
        DependencyProperty.Register(nameof(TextWrapping), typeof(TextWrapping),
            typeof(AnimatedTextBlock), new PropertyMetadata(TextWrapping.NoWrap));

}

Sample page

In my sample page I have added a Button and an AnimatedTextBlock control to the root Grid of the Page. The AnimatedTextBlock is named 'atb1' so i can be used in the code behind of the page.

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="using:App91"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:Controls="using:App91.Views.Controls"
      x:Class="App91.MainPage"
      mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        
        <Button Content="Button"
                HorizontalAlignment="Stretch"
                Height="63"
                Margin="55,64,81,0"
                VerticalAlignment="Top"
                Click="Button_Click" />

        <Controls:AnimatedTextBlock x:Name="atb1"
                                    Margin="32,173,58,0"
                                    VerticalAlignment="Top"
                                    Text="Hello"
                                    Foreground="Red"
                                    TextWrapping="Wrap"
                                    FontSize="48" />

    </Grid>
</Page>

The Button has a Click event which toggles the Text of the AnimatedTextBlock Text from Hello to World.

private bool _showHello;

private void Button_Click(object sender, RoutedEventArgs e) {
    atb1.Text = _showHello ? "Hello" : "World";
    _showHello = !_showHello;
}

Closure and download

I hope you can use this blog for your own Windows 10 apps. You can download the sample project below.

Cheers,

Fons

Download

All postings/content on this blog are provided "AS IS" with no warranties, and confer no rights. All entries in this blog are my opinion and don't necessarily reflect the opinion of my employer or sponsors. The content on this site is licensed under a Creative Commons Attribution By license.

Leave a comment

Blog comments

0 responses