风格翻译变换

发布于 2024-07-25 11:32:59 字数 1326 浏览 3 评论 0原文

我有以下代码:

  <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栖竹 2024-08-01 11:32:59

我在使用 RotateTransform 时遇到了同样的问题。 我希望我的所有控件都具有该动画。 我找到了它的解决方案,看起来它也可以与 TrasnslteTransform 一起使用。

尝试以下代码:

<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="350" Width="525">
<Window.Resources>
    <Style TargetType="{x:Type TextBox}">
        <Setter Property="RenderTransform">
            <Setter.Value>
                <TranslateTransform X="0" Y="0"/>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <EventTrigger RoutedEvent="Loaded">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="(TextBox.RenderTransform).(TranslateTransform.X)"
                                             From="300"
                                             To="-300"
                                             AutoReverse="True"
                                             RepeatBehavior="Forever"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<Grid>
    <TextBox Width="200" Height="22"/>
</Grid>

我想诀窍在于您指定目标属性的方式。 这段代码对我有用。

我知道这个问题提出已经五年了,但其他人可能会从这个答案中受益。 :)

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:

<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="350" Width="525">
<Window.Resources>
    <Style TargetType="{x:Type TextBox}">
        <Setter Property="RenderTransform">
            <Setter.Value>
                <TranslateTransform X="0" Y="0"/>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <EventTrigger RoutedEvent="Loaded">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="(TextBox.RenderTransform).(TranslateTransform.X)"
                                             From="300"
                                             To="-300"
                                             AutoReverse="True"
                                             RepeatBehavior="Forever"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<Grid>
    <TextBox Width="200" Height="22"/>
</Grid>

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. :)

妞丶爷亲个 2024-08-01 11:32:59

不确定我是否遵循它的工作原理,但我认为您需要将 TargetProperty 指定为 X 并将 TargetName 指定为_translateTransform

<Window.Resources>
<Style TargetType="{x:Type TextBlock}">
  <Setter Property="LayoutTransform">
    <Setter.Value>
      <TranslateTransform x:Name="_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="X"
              Storyboard.TargetName="_translateTransform"
              Duration="0:0:1" />
          </Storyboard>
        </BeginStoryboard>
      </EventTrigger.Actions>          
    </EventTrigger>
  </Style.Triggers>      
</Style>

Not sure I follow how it works at all, but I would have thought you'd need to specify the TargetProperty as X and the TargetName as _translateTransform:

<Window.Resources>
<Style TargetType="{x:Type TextBlock}">
  <Setter Property="LayoutTransform">
    <Setter.Value>
      <TranslateTransform x:Name="_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="X"
              Storyboard.TargetName="_translateTransform"
              Duration="0:0:1" />
          </Storyboard>
        </BeginStoryboard>
      </EventTrigger.Actions>          
    </EventTrigger>
  </Style.Triggers>      
</Style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文