Question

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>
Was it helpful?

Solution

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top