Click here to Skip to main content
15,886,008 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
XML
<phone:PhoneApplicationPage.Resources>
        <Storyboard x:Name="Storyboard1">
            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="textBlock">
                <EasingColorKeyFrame KeyTime="0:0:2.5" Value="Red"/>
                <EasingColorKeyFrame KeyTime="0:0:5" Value="Blue"/>
                <EasingColorKeyFrame KeyTime="0:0:7.5" Value="Red"/>
                <EasingColorKeyFrame KeyTime="0:0:10" Value="White"/>
            </ColorAnimationUsingKeyFrames>
        </Storyboard>
    </phone:PhoneApplicationPage.Resources>
Posted
Comments
gaurav273 21-Jul-12 0:26am    
i am in great problem,actually i want that,the color of my text present in textblock should change colour character by character,like if my text is ABCDE,so first A should change color,then B should change color.....AND SO ON,BUT BY USING THIS method,whole of the text colour is changing at once,so please tell me any solution?
[no name] 21-Jul-12 0:26am    
And?
Sebastian T Xavier 21-Jul-12 0:31am    
What is this?

1 solution

I think, you cant use this way;) because foreground is property for all of the text in the TextBlock.
My solution:
Create 'Array' of TextBlock for example:
C#
TextBlock[] tb = new TextBlock[5];
then create a timer and finally with every tick event, change foreground one of array element.

Updated program in XAML:
XML
<Window x:Class="WpfApp_ColourTextBlock.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:System="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525" WindowStartupLocation="CenterScreen">
    <Window.Resources>
        <System:String x:Key="textBlockName">textBlock0</System:String>
        <Storyboard x:Key="Storyboard1">
            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="{DynamicResource textBlockName}">
                <EasingColorKeyFrame KeyTime="0:0:2.5" Value="Red"/>
                <EasingColorKeyFrame KeyTime="0:0:5" Value="Blue"/>
                <EasingColorKeyFrame KeyTime="0:0:7.5" Value="Red"/>
                <EasingColorKeyFrame KeyTime="0:0:10" Value="White"/>
            </ColorAnimationUsingKeyFrames>
        </Storyboard>
    </Window.Resources>
    <Viewbox>
        <StackPanel Name="TextBlocks" Orientation="Horizontal">

        </StackPanel>
    </Viewbox>
</Window>

And code behind in C#:
C#
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace WpfApp_ColourTextBlock
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            counter = 0;
            foreach (char item in "Hello Word".ToArray())
            {
                TextBlock tb = new TextBlock();
                tb.Name = "textBlock" + counter++;
                this.RegisterName(tb.Name, tb);
                tb.Text = item.ToString();
                tb.Foreground = Brushes.DarkCyan;
                TextBlocks.Children.Add(tb);
            }

            counter = 0;

            System.Windows.Threading.DispatcherTimer myDispatcherTimer = new System.Windows.Threading.DispatcherTimer();
            myDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000); // 100 Milliseconds 
            myDispatcherTimer.Tick += new EventHandler(myDispatcherTimer_Tick);
            myDispatcherTimer.Start();
        }

        int counter;

        void myDispatcherTimer_Tick(object sender, EventArgs e)
        {
            Resources["textBlockName"] = "textBlock" + counter++;
            (Resources["Storyboard1"] as Storyboard).Begin();

            if (counter >= TextBlocks.Children.Count)
                counter = 0;
        }
    }
}
 
Share this answer
 
v3
Comments
gaurav273 23-Jul-12 5:42am    
I have my the timer and the TextBlock array,but its not Implememnting through it.can YOU PLEASE give me the code
gaurav273 23-Jul-12 5:43am    
public void StartTimer(object o, RoutedEventArgs sender)
{
System.Windows.Threading.DispatcherTimer myDispatcherTimer = new System.Windows.Threading.DispatcherTimer();
myDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000); // 100 Milliseconds
myDispatcherTimer.Tick += new EventHandler(Each_Tick);
myDispatcherTimer.Start();
}


int i = 0;
// Raised every 100 miliseconds while the DispatcherTimer is active.
public void Each_Tick(object o, EventArgs sender)
{
Color r = Colors.Red;
textBlock.Foreground= new SolidColorBrush(r);
}
gaurav273 23-Jul-12 5:43am    
TextBlock[] tb = new TextBlock[500];
gaurav273 23-Jul-12 5:44am    
how to use the array,to change the color of characters so it should look like moving with the audio playing in the background
hzawary 23-Jul-12 16:17pm    
I'm glad to accept my solution and thanks:)
Is solved your problem?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900