textblock文字滚动效果

huihowie 2011-12-13 01:42:44
<VisualState x:Name="MouseOver">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="ButtonDelete">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Collapsed}"/>
<DiscreteObjectKeyFrame KeyTime="0:0:0.001" Value="{x:Static Visibility.Visible}"/>
</ObjectAnimationUsingKeyFrames>

<ColorAnimation To="LightSlateGray" Duration="0:0:0.2" Storyboard.TargetProperty="(ListBoxItem.Background).(SolidColorBrush.Color)"/>

</Storyboard>
</VisualState>
要在这个storyboard里让ListBox里面的textblock实现文字滚动效果 应该怎么写?
...全文
489 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
huihowie 2011-12-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 fallincloud 的回复:]
一个例子:
自定义TextBlock如下:

C# code
public class TextBlockTest :TextBlock
{
TranslateTransform tt;
bool animated;
public TextBlockTest()
: base()
{……
[/Quote]
我以为在storyboard里像 <ColorAnimation To="LightSlateGray" Duration="0:0:0.2" Storyboard.TargetProperty="(ListBoxItem.Background).(SolidColorBrush.Color)"/>
写句话就搞定了 o(╯□╰)o
fallincloud 2011-12-13
  • 打赏
  • 举报
回复
一个例子:
自定义TextBlock如下:
public class TextBlockTest :TextBlock
{
TranslateTransform tt;
bool animated;
public TextBlockTest()
: base()
{
tt = new TranslateTransform();
this.RenderTransform = tt;
animated = false;
this.MouseEnter += new MouseEventHandler(TextBlockTest_MouseEnter);

}


void TextBlockTest_MouseEnter(object sender, MouseEventArgs e)
{
if (tt == null)
return;
if (animated)
return;

Duration duration = new Duration(TimeSpan.FromSeconds(0.5));

DoubleAnimation da = new DoubleAnimation(0,-30, duration);
da.AutoReverse = true;

tt.BeginAnimation(TranslateTransform.XProperty, da);
animated = true;


}




}

xaml很简单
<ListBox x:Name="ListBoxTest">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Height="50" Background="AliceBlue">
<local:TextBlockTest Text="{Binding}"></local:TextBlockTest>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

随便指定一个source:
string[] source = new string[] { "Alpha", "Beta","Crow"};
ListBoxTest.ItemsSource = source;
huihowie 2011-12-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 bunliney 的回复:]
如何滚动?上下/左右?循环滚动还是滚动一次?
[/Quote]
从右到左循环滚动
Bullatus 2011-12-13
  • 打赏
  • 举报
回复
如何滚动?上下/左右?循环滚动还是滚动一次?
huihowie 2011-12-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fallincloud 的回复:]
先弄出个能运行的:遍历ListBox的Items找到你想要滚动的那个Textblock,然后让TextBlock执行动画,
接下来重构。
[/Quote]
具体该怎么搞 能贴个出来吗 大哥
fallincloud 2011-12-13
  • 打赏
  • 举报
回复
先弄出个能运行的:遍历ListBox的Items找到你想要滚动的那个Textblock,然后让TextBlock执行动画,
接下来重构。
资源下载链接为: https://pan.quark.cn/s/abbae039bf2a 在 WPF 中,TextBlock 是用于显示文本的控件。当文本内容超出控件显示范围时,可以通过动画和故事板(Storyboard)实现文本的自动滚动效果。 通过 TextAlignment 实现滚动 默认情况下,TextBlock 的内容是左对齐的。可以将其设置为右对齐(Right),然后通过动画改变其偏移量,让文本从右向左滚动。具体实现是创建一个 DoubleAnimation,在一定时间内将偏移量从最右端移动到最左端,完成滚动效果。 通过 TextTrimming 和 ScrollViewer 实现滚动TextBlock 放入 ScrollViewer 中,设置 TextTrimming 为 CharacterEllipsis。然后通过动画改变 ScrollViewer 的 HorizontalOffset 属性来模拟滚动效果。这种方式可以让文本在控件内自动滚动。 以下是一个简单的 XAML 示例,展示如何实现 TextBlock 的自动滚动动画: 在此示例中,当窗口加载时,HorizontalOffset 从 0 变化到 100,整个过程持续 5 秒,并无限重复,从而实现文本的自动滚动。 实际开发中,可以根据需求调整动画参数,如滚动速度、方向以及是否循环。 为了确保动画流畅,还需考虑性能优化,避免频繁的 UI 更新导致性能瓶颈。 如果需要更详细的实现方式,可以参考示例项目 TextBlockAnimation,其中包含了完整的代码和资源。你可以下载并运行该项目,学习其实现方式,并根据需求进行定制和扩展。

8,757

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧