Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WinMobile WPF
<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 20-Jul-12 19:23pm
Comments
gaurav273 at 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?
Wes Aday at 21-Jul-12 0:26am
   
And?
Sebastian T Xavier at 21-Jul-12 0:31am
   
What is this?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
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:
<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#:
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;
        }
    }
}
  Permalink  
v3
Comments
gaurav273 at 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 at 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 at 23-Jul-12 5:43am
   
TextBlock[] tb = new TextBlock[500];
gaurav273 at 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 at 23-Jul-12 16:17pm
   
I'm glad to accept my solution and thanks:)
Is solved your problem?
gaurav273 at 24-Jul-12 1:08am
   
actually,your method is gud,but am not able to implement it fully,i have made 1 timer and array of my textblock,but still its changing colour of complete text,so can you write the code here.
hzawary at 25-Jul-12 14:19pm
   

Please wait! receive your answer! I doing work on the issue:p
gaurav273 at 26-Jul-12 1:53am
   
ohkk..m waiting
gaurav273 at 26-Jul-12 1:58am
   
public void Each_Tick(object o, EventArgs sender)
{
x++;
Color r = Colors.Red;
if (x < 5)
{
 
tb[0] = new TextBlock();
tb[0].Text = "g";
tb[0].Foreground = new SolidColorBrush(r);
 
tb[1] = new TextBlock();
tb[1].Text = "a";
tb[1].Foreground = new SolidColorBrush(r);
 
tb[2] = new TextBlock();
tb[2].Text = "u";
tb[2].Foreground = new SolidColorBrush(r);
 
tb[3] = new TextBlock();
tb[3].Text = "r";
tb[3].Foreground = new SolidColorBrush(r);
 
tb[4] = new TextBlock();
tb[4].Text = "a";
tb[4].Foreground = new SolidColorBrush(r);
}
}
gaurav273 at 26-Jul-12 1:58am
   
itss not wrking
hzawary at 26-Jul-12 11:35am
   
Ok! finally resolved, Here you are.
The program and source it downloadable from this link:
http://www.4shared.com/rar/gtHoWjWd/WpfApp_ColourTextBlock.html
gaurav273 at 27-Jul-12 3:25am
   
i am making this application in windows mobile 7,can u send me the coding,according to the windowsphone7.IT'S URGENT
hzawary at 27-Jul-12 9:12am
   
You don't expect from me that I wrote your program, exactly! so I just explain solution and update it in XAML code and code behind in C#!
gaurav273 at 28-Jul-12 1:13am
   
ohk,thanku so much
hzawary at 28-Jul-12 15:39pm
   
What's result?! Is run the codes?
gaurav273 at 30-Jul-12 1:51am
   
no no...i am still trying,ohkk jst tell me 1 thing,we use this.RegisterName(tb.Name, tb); in wpf........BUT WHAT we ll USE IN WINDOWS PHONE 7?OR HOW TO USE IT??JST TELL ME
hzawary at 30-Jul-12 6:16am
   
There is no 'RegisterName' method In windows phone 7 nowise?
gaurav273 at 30-Jul-12 6:46am
   
but. u have used it in ur application ,,so how can i implmnt it in my wp7 application nd how to make system string in wp7??pls tell me
hzawary at 30-Jul-12 15:05pm
   
Best job is that you discuss about this issue in wp7 forum, I'm sorry because didn't help more:(
Good luck:)
gaurav273 at 31-Jul-12 0:37am
   
ohk...thnku

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

  Print Answers RSS
0 OriginalGriff 386
1 Marcin Kozub 225
2 Sergey Alexandrovich Kryukov 215
3 Maciej Los 169
4 /\jmot 169
0 OriginalGriff 8,289
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,624
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 27 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100