views:

539

answers:

1

Hi,

I've set up a Transformation and a Storyboard as local resources, but when I trigger the storyboard from a button I get the following error:

{"'WorldTranslation' name cannot be found in the name scope of 'System.Windows.Controls.Button'."}

Can someone point me in the right direction please?

<Window.Resources>

    <Transform3DGroup x:Key="WorldTranslation">
        <RotateTransform3D>
            <RotateTransform3D.Rotation>
                <AxisAngleRotation3D x:Name="myAngleRotation" Axis="0,1,0" Angle="0" />
            </RotateTransform3D.Rotation>
        </RotateTransform3D>
    </Transform3DGroup>

    <Storyboard x:Key="MyStoryboard">
        <DoubleAnimation
            Storyboard.TargetName="WorldTranslation"
        Storyboard.TargetProperty="(Transform3DGroup).(RotateTransform3D).(RotateTransform3D.Rotation).(AxisAngleRotation3D.Angle)"
        From="0"
        To="360"
        Duration="0:0:1"   
       />
    </Storyboard>

</Window.Resources>

And heres the button xaml ...

<Button Height="20" Width="100">
   <Button.Content>Blah</Button.Content>
       <Button.Triggers>
          <EventTrigger RoutedEvent="Button.Click">
               <EventTrigger.Actions>
                   <BeginStoryboard Storyboard="{StaticResource MyStoryboard}" >

                    </BeginStoryboard>
                </EventTrigger.Actions>
          </EventTrigger>
       </Button.Triggers>
  </Button>
+3  A: 

Hi, There are several things wrong in you XAML. I will start with the transform inline and then refactor it to resources. I changed to a 2D RotateTransform as not to complicate matters unnecessarily.

We start with:

       <Button Height="20" Width="100"
            RenderTransformOrigin=".5,.5"
            Content="Blah">
        <Button.RenderTransform>
            <RotateTransform Angle="0" x:Name="MyAnimatedTransform"/>
        </Button.RenderTransform>
        <Button.Triggers>
            <EventTrigger RoutedEvent="Button.Click">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation
                              Storyboard.TargetName="MyAnimatedTransform"
                              Storyboard.TargetProperty="(RotateTransform.Angle)"
                              From="0.0" To="360" Duration="0:0:1" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </Button.Triggers>
    </Button>

Then we take out the StoryBoard:

<Window.Resources>
    <Storyboard x:Key="MyStoryBoard">
        <DoubleAnimation
           Storyboard.TargetName="MyAnimatedTransform"
           Storyboard.TargetProperty="(RotateTransform.Angle)"
           From="0.0" To="360" Duration="0:0:.3" />
    </Storyboard>
</Window.Resources>
<Grid>
    <Button Height="20" Width="100"
            RenderTransformOrigin=".5,.5"
            Content="Blah">
        <Button.RenderTransform>
            <RotateTransform Angle="0" x:Name="MyAnimatedTransform"/>
        </Button.RenderTransform>
        <Button.Triggers>
            <EventTrigger RoutedEvent="Button.Click">
                <EventTrigger.Actions>
                    <BeginStoryboard Storyboard="{StaticResource MyStoryBoard}"/>
                </EventTrigger.Actions>
            </EventTrigger>
        </Button.Triggers>
    </Button>
</Grid>

And finally the RotateTransform. Referencing this by the Storyboard.TargetName property does not work here. You need the Storyboard.Target property:

<Window.Resources>
    <RotateTransform Angle="0" x:Key="WorldTransform"/>
    <Storyboard x:Key="MyStoryBoard">
        <DoubleAnimation
           Storyboard.Target="{Binding TemplatedParent}"
           Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)"
           From="0.0" To="360" Duration="0:0:.3" />
    </Storyboard>
</Window.Resources>
<Grid>
    <Button Height="20" Width="100"
            RenderTransformOrigin=".5,.5"
            Content="Blah">
        <Button.RenderTransform>
            <StaticResource ResourceKey="WorldTransform" />
        </Button.RenderTransform>
        <Button.Triggers>
            <EventTrigger RoutedEvent="Button.Click">
                <EventTrigger.Actions>
                    <BeginStoryboard Storyboard="{StaticResource MyStoryBoard}"/>
                </EventTrigger.Actions>
            </EventTrigger>
        </Button.Triggers>
    </Button>
</Grid>

EDIT: due to bonus question ;-) Suppose you want the transform to take place on the containing grid. Simply catch the Button.Click event in the Grid (events are routed remember?) and put the trigger there. The TemplatedParent will now be the grid and no longer the button.

 <Grid RenderTransformOrigin=".5,.5">
    <Grid.RenderTransform>
        <StaticResource ResourceKey="WorldTransform"/>
    </Grid.RenderTransform>
    <Grid.Triggers>
        <EventTrigger RoutedEvent="Button.Click">
            <EventTrigger.Actions>
                <BeginStoryboard Storyboard="{StaticResource MyStoryBoard}"/>
            </EventTrigger.Actions>
        </EventTrigger>
    </Grid.Triggers>
    <Button Height="20" Width="100"
            Content="Blah">
    </Button>
</Grid>
Dabblernl
Hi, thank you for your assistance, in your solution though your performing the translation on TemplatedParent which is the Button - I need to get back to Buttons parent. I understand how FindAncestor works, but I don't know how to bring it together with TemplatedParent. In your example, could you show me how you'd apply the storyboard to the Grid, rather than the button please? Thanks for your help
Andy Clarke
No problem, I edited my answer.
Dabblernl
Many thanks, thats a great help!
Andy Clarke