Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TranslateTransform in Style

Tags:

wpf

transform

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?


1 Answers

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

like image 155
Stefan Vasiljevic Avatar answered Feb 16 '26 22:02

Stefan Vasiljevic