How to animate a custom control?

As far as I know, the DependencyProperty could be animated, it's simple to use this feature in XAML, we could indicate the animation target name like:
"Rectangle.Width", etc. but when I use a custom control, say "MyButton", it has a DependencyProperty "Value", I could use "MyButton" in XAML like:

<Window x:Class="MyUserControl.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:uc="clr-namespace:MyUserControl">
<Grid>
<uc:MyButton/>
</Grid>


but how could I set the Animation TargetProperty for MyButton.Value I do it by this way:

<Window x:Class="MyUserControl.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:uc="clr-namespace:MyUserControl">
<Window.Triggers>
<EventTrigger RoutedEvent="Window.Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DecimalAnimation Storyboard.TargetName="myNumericUpDown" Storyboard.TargetProperty="uc:NumericUpDown.Value" From="0" To="100" RepeatBehavior="Forever"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Window.Triggers>
<Grid>
<uc:MyButton x:Name="myButton" Value="10"/>
</Grid>
</Window>


But I got a exception says that TargetProperty is wrong, so how could I fix this problem

Anyone help thanks!


Answer this question

How to animate a custom control?

  • Hugo Bara&amp;#250;na

    use:

    Storyboard.TargetProperty="(uc:NumericUpDown.Value)"


  • NewBieDJ

    That works! Thanks a lot!!!
  • How to animate a custom control?