tags:

views:

92

answers:

1

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?

A: 

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>

HTH, Kent

Kent Boogaart
That's what I tried first but apparently you can't set the property Storyboard.TargetName in a Style.