wpfelementname

Binding ElementName


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 .


Solution

  • 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!