views:

18

answers:

1

I've been struggling with this for hours and I can't find out what I'm doing wrong. Please help me find my mistake.

I created a user control with one custom dependency property and I want to animate this property.

Here is my class:

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    public long Val
    {
        get { return (long)GetValue(ValProperty); }
        set {
             SetValue(ValProperty, value);

             // Update a text block for debug
             ((Class1)this.Resources["class1"]).Val = value;  
        }
    }

    public static readonly DependencyProperty ValProperty =
        DependencyProperty.Register("Val", typeof(long), typeof(UserControl1), new UIPropertyMetadata(0L));
}

Here is the code that is supposed to animate it (there's an instance of UserControl1 called usercontrol11):

        Int64Animation myAnimation = new Int64Animation();
        myAnimation.From = 100;
        myAnimation.To = 200;
        myAnimation.Duration = new Duration(TimeSpan.FromSeconds(1));

        Storyboard.SetTargetName(myAnimation, userControl11.Name);
        Storyboard.SetTargetProperty(myAnimation, new PropertyPath(UserControl1.ValProperty));

        Storyboard myStoryboard = new Storyboard();
        myStoryboard.Children.Add(myAnimation);
        myStoryboard.Begin(this);

I also tried the XAML approach, but it didn't work either (in the following XAML, the Width animation works fine, but the Val doesn't):

<my:UserControl1 HorizontalAlignment="Left" Width="150" Margin="72,45,0,0" x:Name="userControl11" VerticalAlignment="Top" Background="#FFFFD100">
  <my:UserControl1.Triggers>
    <EventTrigger RoutedEvent="MouseEnter">
        <EventTrigger.Actions>
            <BeginStoryboard>
                <Storyboard>
                    <Int64Animation To="300" Duration="0:0:1"
                                    Storyboard.TargetProperty="Val" />
                    <DoubleAnimation To="300" Duration="0:0:1"
                                    Storyboard.TargetProperty="Width" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger.Actions>
    </EventTrigger>                        
  </my:UserControl1.Triggers>
</my:UserControl1>

Any help will be much appreciated!

+2  A: 

Well, it's actually animated. You're just checking in the wrong place, animation won't call Val property set accessor. You can, for example, override OnPropertyChanged method to see changes:

    protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
    {
        base.OnPropertyChanged(e);
        if(e.Property == ValProperty)
        {
            MessageBox.Show(e.NewValue.ToString());
        }
    }
max
Wow, thank you so much, it worked!But why does it work this way? It seems very strange to me. I thought the point of animation is to change the value of the property... How come OnPropertyChanged is called, but the value does not get changed automatically?
Ilya Kogan