locked
How to set Storyboard's TargetProperty to custom Dependency Property of ItemTemplate's DataContext?

    Question

  • A little complicated scenario :)

    My ItemsControl is bound to a list of a DependencyObject and inside the ItemTemplate I have a storyboard.
    I want this storyboard TargetProperty, to bind to a dependency property (named Left) of the DepedencyObject (which is the DataContext of the item).

    I'm getting this error: "Cannot resolve TargetProperty Left on specified object."
    I tried a lot of different bindings but all return the same error.

    This is how it looks:

    xaml

    <ItemsControl ItemsSource="{Binding Path=Items}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Canvas />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                            <Path Data="{Binding PathGeometry}"
                                x:Name="path">
                                <Path.Triggers>
                                    <EventTrigger RoutedEvent="Path.Loaded">
                                        <BeginStoryboard>
                                            <Storyboard>
                                                <DoubleAnimation
                                                    EnableDependentAnimation="True"
                                                    Storyboard.TargetName="path"
                                                    Storyboard.TargetProperty="Left"
                                                    From="-200"
                                                    To="300"
                                                    Duration="0:0:10"
                                                    RepeatBehavior="Forever"
                                                    />
                                            </Storyboard>
                                        </BeginStoryboard>
                                    </EventTrigger>
                                </Path.Triggers>
                            </Path>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

    The DependencyObject

        public class SomeDependencyObject : DependencyObject
        {
            public static DependencyProperty LeftProperty = DependencyProperty.Register("Left", typeof(double), typeof(SomeDependencyObject), new PropertyMetadata(0));

            public double Left
            {
                get
                {
                    return (double)this.GetValue(SomeDependencyObject.LeftProperty);
                }
                set
                {
                    this.SetValue(SomeDependencyObject.LeftProperty, value);
                }
            }

            // .... Some more Code .... //

            public SomeDependencyObject()
            {

            }
        }
    }


    Thanks ahead!

    Saturday, May 10, 2014 11:14 AM

All replies

  • Hi, please try changing

    Storyboard.TargetName="path"
    to
    Storyboard.Target="{Binding}"

    Saturday, May 10, 2014 11:42 AM
  • I get this error:

    Type 'Binding' used after '{' must be a Markup Extension

    Saturday, May 10, 2014 11:56 AM