I'm trying to create an automatic scrolling textblock.
This is the code I have so far:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="150" Width="400">
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="50"></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel Background="Red" Grid.Column="0" />
<StackPanel x:Name="UserContainer" Grid.Column="1">
<TextBlock x:Name="UserStatusLabel" TextWrapping="NoWrap" VerticalAlignment="Top"><Run Language="nl-nl" Text="Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit."/>
<TextBlock.RenderTransform>
<TranslateTransform x:Name="translate" />
</TextBlock.RenderTransform>
<TextBlock.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard>
<Storyboard RepeatBehavior="Forever">
<DoubleAnimation
From="{Binding ElementName=UserContainer, Path=Width}" To="-200"
Storyboard.TargetName="translate"
Storyboard.TargetProperty="X"
Duration="0:0:3"
AutoReverse="True" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock.Triggers>
</TextBlock>
</StackPanel>
<StackPanel Background="Blue" Grid.Column="2" />
</Grid>
But I have 3 problems:
- The text should be scrolled to the left till the end. Now it stops at a certain point
- The text should not cross the red container at the left, it shouldn't run trough it.
- The text is cut off at a certain point, but I don't know why.
First, you need to add
ClipToBounds="True"
to you centralStackPanel
to stop the text from showing after it leaves the bounds of theStackPanel
.Secondly, the text stops at a 'certain distance' as you say because you told it to by setting the
DoubleAnimation
to only move to-200
. We need to replace that with the actual value of theStackPanel.ActualWidth
property:However, this moves the text to the right edge instead. I'm going to give this to you as an answer for now and if I can think of an easy way to reverse the direction of movement, I'll come back and edit it later.
UPADTE >>>
Ok, so the easiest thing to make the text move left is to implement a simple
Converter
:Then change your XAML:
Don't forget to add the XML Namespace:
And
Resource
:UPDATE 2 >>>
Ah, I just worked it out... the
TextBlock
is cut off because it won't fit into theStackPanel
otherwise... making theStackPanel
bigger makes more text appear. However, the answer is to not use theStackPanel
and use aCanvas
instead... this shows all of the text.