views:

154

answers:

1

Hello First Sorry for my English. I wanted to ask why ElementName does not work the first case, and work in the second.

I give the two sections of code . the firts not work

<Button                         
         Name="button1" Width="100" >
        <Button.LayoutTransform>
            <ScaleTransform x:Name="ttt" ScaleX="3" ScaleY="6"/>
        </Button.LayoutTransform>
        <Button.Triggers>
            <EventTrigger RoutedEvent="Path.Loaded">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard RepeatBehavior="Forever">
                            <DoubleAnimation
                                Storyboard.Target="{Binding ElementName=ttt}"
                                Storyboard.TargetProperty="ScaleX"
                                From="10"
                                To="5"
                                Duration="0:0:1"
                                />                                
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </Button.Triggers>
        Button
    </Button>

But it does work

<Button                         
         Name="button1" Width="100" >
        <Button.LayoutTransform>
            <ScaleTransform x:Name="ttt" ScaleX="3" ScaleY="6"/>
        </Button.LayoutTransform>
        <Button.Triggers>
            <EventTrigger RoutedEvent="Path.Loaded">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard RepeatBehavior="Forever">
                            <DoubleAnimation
                                Storyboard.Target="{Binding ElementName=button1}"
                                Storyboard.TargetProperty="Width"
                                From="100"
                                To="50"
                                Duration="0:0:1"
                                />                                
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </Button.Triggers>
        Button
    </Button>

I know I can use Storyboard.TargetName .

+1  A: 

I think your problem is that a ScaleTransform is not part of the Visual Tree, and therefore can not be found by the {Binding ElementName=ttt} expression.

However, you could try using the following code instead:

Storyboard.TargetName="ttt"

Hope this works. Good luck!

gehho
But why it is not in Visual Tree ? and if it is not, why Storyboard.TargetName did work?Thanks
zvi
ScaleTransform is not a FrameworkElement, and therefore cannot be used as source for Binding.ElementName. Storyboard.TargetName works because this property also accepts items other than FrameworkElements. There is no Binding involved in this case. I think the only restriction is that it must be a DependencyObject, and ScaleTransform is a DependencyObject. However, I am not sure on this one...
gehho
Very interesting, thanks
zvi