如何在xaml的文本块中添加滚动/移动文本
本文关键字:文本 添加 滚动 移动 xaml | 更新日期: 2023-09-27 17:50:59
我想在textblock中添加一个滚动/移动文本(从右到左)。它应该只滚动一次。我在谷歌上搜索了一下,但没有找到任何东西。我想滚动文本块(不是整个文本块)的文本只有一次。然后应该停止滚动
我在网上找到了这个代码,但它不是我想要的。我想滚动文本1次,然后停止滚动。知道怎么做吗?
<TextBlock FontSize="22" x:Name="txtScrolling" Margin="1386,208,-616,460">
<TextBlock.RenderTransform>
<TranslateTransform x:Name="translate" />
</TextBlock.RenderTransform>
<TextBlock.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard>
<Storyboard RepeatBehavior="1">
<DoubleAnimation
From="1000" To="-1000"
Storyboard.TargetName="translate"
Storyboard.TargetProperty="X"
Duration="0:0:10" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock.Triggers>
This is the Text to Scroll
</TextBlock>
你可以试试这样做…
这是一个示例:
<Grid>
<ScrollViewer x:Name="Scroll_Content" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Auto" CanContentScroll="True" VerticalAlignment="Center" HorizontalAlignment="Center" Width="250" FlowDirection="RightToLeft">
<TextBlock Text="Hello World ------------ Hello World ------------ Hello World -------- Hello World"></TextBlock>
</ScrollViewer>
<Button x:Name="Start_Timer" VerticalAlignment="Bottom" HorizontalAlignment="Center" Margin="0,0,0,10" Width="100" Height="50" Content="Start Timer" Click="Start_Timer_Click"/>
</Grid>
背后的代码:
DispatcherTimer timer1 = new DispatcherTimer();
double num = 0;
public MainWindow()
{
InitializeComponent();
timer1.Interval = new TimeSpan(0,0,0,0,250);
timer1.Tick += timer1_Tick;
}
void timer1_Tick(object sender, EventArgs e)
{
try
{
Scroll_Content.ScrollToHorizontalOffset(num);
num++;
}
catch { }
}
void Start_Timer_Click(object sender, RoutedEventArgs e)
{
if (timer1.IsEnabled == false)
{
num = 0;
timer1.Start();
}
else
timer1.Stop();
}