风格翻译变换
我有以下代码:
<Window.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="LayoutTransform">
<Setter.Value>
<TranslateTransform />
</Setter.Value>
</Setter>
<Style.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard RepeatBehavior="Forever" AutoReverse="True">
<DoubleAnimation
From="300"
To="-300"
Storyboard.TargetProperty="LayoutTransform.X"
Duration="0:0:1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>
<TextBlock
Grid.Column="1"
Text="This is a sample text."/>
<Rectangle Grid.Column="0" Fill="AliceBlue"/>
<Rectangle Grid.Column="2" Fill="Aquamarine"/>
基本上我想要实现的是 TextBlock 的内容应该从右向左(然后向后)滚动。 不知何故,这种风格没有做任何事情。 如果我将 TranslateTransform 更改为 ScaleTransform 并将 LayoutTransform.X 更改为 LayoutTransform.ScaleX,则 TextBlock 的动画效果就很好。 这是 WPF 中的错误还是我遗漏了什么?
I have the following code:
<Window.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="LayoutTransform">
<Setter.Value>
<TranslateTransform />
</Setter.Value>
</Setter>
<Style.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard RepeatBehavior="Forever" AutoReverse="True">
<DoubleAnimation
From="300"
To="-300"
Storyboard.TargetProperty="LayoutTransform.X"
Duration="0:0:1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>
<TextBlock
Grid.Column="1"
Text="This is a sample text."/>
<Rectangle Grid.Column="0" Fill="AliceBlue"/>
<Rectangle Grid.Column="2" Fill="Aquamarine"/>
Basically what I'm trying to achieve is that the content of TextBlock should be scrolling from right to left (and back). Somehow this Style doesn't do anything.
If I change TranslateTransform to ScaleTransform and change LayoutTransform.X to LayoutTransform.ScaleX the TextBlock is animated just fine.
Is this a bug in WPF or am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在使用 RotateTransform 时遇到了同样的问题。 我希望我的所有控件都具有该动画。 我找到了它的解决方案,看起来它也可以与 TrasnslteTransform 一起使用。
尝试以下代码:
我想诀窍在于您指定目标属性的方式。 这段代码对我有用。
我知道这个问题提出已经五年了,但其他人可能会从这个答案中受益。 :)
I had the same problem with RotateTransform. I have wanted all my controls to have that animation. I found the solution for it, and it looks like it is working with TrasnslteTransform as well.
Try out the following code:
I guess the trick is in the way you specify your target property. This code worked for me.
I know it has been 5 years since the question was posed, but someone else may benefit from the answer. :)
不确定我是否遵循它的工作原理,但我认为您需要将
TargetProperty
指定为X
并将TargetName
指定为_translateTransform
:Not sure I follow how it works at all, but I would have thought you'd need to specify the
TargetProperty
asX
and theTargetName
as_translateTransform
: