Click here to Skip to main content
15,902,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I am new to WPF my question is how to auto move a label left to right in WPF application


Thanks & regards
Indrajit
Posted
Comments
Fredrik Bornander 7-Feb-13 4:10am    
Do you mean you want to animate it's position so that it moves to the left?
IndrajitDasgupat 8-Feb-13 0:30am    
I mean in C# windows application we are using time_Tick event below is code

private void timer1_Tick(object sender, EventArgs e)
{
label1.Location = new Point(label1.Location.X + 5, label1.Location.Y);

if(label1.Location.X > this.Width)
{
label1.Location = new Point(0 - label1.Width, label1.Location.Y);
}
}
Now how can I do same thing in WPF

1 solution

void Window1_Loaded(object sender, RoutedEventArgs e)

{

DoubleAnimation doubleAnimation = new DoubleAnimation();

doubleAnimation.From = -tbmarquee.ActualWidth;

doubleAnimation.To = canMain.ActualWidth;

doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;

doubleAnimation.Duration = new Duration(TimeSpan.Parse("0 : 0 : 10"));

tbmarquee.BeginAnimation(Canvas.LeftProperty, doubleAnimation);

}

// xaml

<window x:class="MarqueText.Window1" xmlns:x="#unknown">

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Window1" Height="500" Width="700" >

<canvas background="White">

<canvas margin="50" canvas.left="150" canvas.top="100" cliptobounds="True" name="canMain" background="Red" height="100" width="300">

<textblock margin="0,40" fontsize="25" name="tbmarquee">Bangladesh

</canvas>

</canvas>





For Left to right marquee: use Left Property of Canvas

For right to left marquee: use Right Property of Canvas

For upward marquee: use Bottom Property of Canvas

For Downward marquee: use Top Property of Canvas

Or



<textblock grid.row="2" fontsize="22" name="txtScrolling">
<textblock.rendertransform>
<translatetransform x:name="translate" xmlns:x="#unknown">
<textblock.triggers>
<eventtrigger routedevent="FrameworkElement.Loaded">
<beginstoryboard>
<storyboard repeatbehavior="Forever">
<doubleanimation>
From="1200" To="-1200"
Storyboard.TargetName="translate"
Storyboard.TargetProperty="X"
Duration="0 : 0 :9" />




This is the Text to Scroll
 
Share this answer
 
v3

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