开发者

I can't animate a custom property in WPF

开发者 https://www.devze.com 2023-01-19 14:26 出处:网络
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'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 UIPropert开发者_Python百科yMetadata(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!


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());
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消