1
我一直在爲此奮鬥了好幾個小時,我找不到我做錯了什麼。請幫我找到我的錯誤。我無法爲WPF中的自定義屬性設置動畫
我用一個自定義依賴項屬性創建了一個用戶控件,我想爲此屬性設置動畫。
這裏是我的類:
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));
}
這裏是一個應該製作動畫(有一個名爲usercontrol11的UserControl1的實例)的代碼:
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);
我也嘗試了XAML的做法,但它也不起作用(在以下XAML中,寬度動畫可以正常工作,但Val不會):
<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>
任何幫助將不勝感激!
哇,非常感謝您的工作! 但是它爲什麼這樣工作?對我來說這似乎很奇怪。我認爲動畫的重點是要改變屬性的值...如何調用OnPropertyChanged,但該值不會自動更改? – 2010-10-10 05:43:10