我有财产:
double _X;
public double X
{
get {
this.Title = _X.ToString();
return _X;
}
set {
_X = value;
this.Title = _X.ToString(); // !!!! this line does not execute when property changes from storyboard
RaisePropertyChanged("X"); }
}
和控制:
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Height="99.2" Margin="37,14,0,0" VerticalAlignment="Top" Width="98.4" RenderTransformOrigin="0.5,0.5">
<Button.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform X="{Binding X}" Y="{Binding Y}"/>
</TransformGroup>
</Button.RenderTransform>
</Button>
故事板:
<Storyboard x:Key="Storyboard1" AutoReverse="True" FillBehavior="HoldEnd">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="button">
<EasingDoubleKeyFrame KeyTime="0:0:1.2" Value="100">
<EasingDoubleKeyFrame.EasingFunction>
<CircleEase EasingMode="EaseOut"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
当我为属性设置动画时,如果我正在实现 INotifyPropertyChanged 接口,为什么 set 方法不会改变?换句话说,为什么在this.Title = _X.ToString();
动画故事板的同时不执行该行。每次故事板动画时如何更新 X 属性?
编辑
我忘了提到我需要这个功能的原因。我经常需要为不在 xaml 中的属性设置动画。例如,假设我想为主音量或鼠标位置设置动画。我知道我可以创建一个线程然后进行数学运算并创建自己的动画。但是如果 wpf 已经有几个缓动功能和动画,为什么还要重新发明轮子。如果我可以使用表达式混合创建一个情节提要,然后使用该情节提要在我的代码中为属性设置动画,那就太好了。
编辑2
根据@clemens,我已经解决了这个问题(也许我做错了什么):
我有课:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.ComponentModel;
using System.Windows;
namespace SomeNamespace
{
public partial class MyUserControl : UserControl, INotifyPropertyChanged
{
public static readonly DependencyProperty SomeDoubleProperty =
DependencyProperty.Register("SomeDouble", typeof(Double),
typeof(MyUserControl), new PropertyMetadata(0.0));
public double SomeDouble
{
get
{
return (double)GetValue(SomeDoubleProperty);
}
set
{
SetValue(SomeDoubleProperty, value);
MessageBox.Show("this msg should appear meanwhile animating!");
// code does not execute
RaisePropertyChanged("SomeDouble");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
我有观点
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:test="clr-namespace:SomeNamespace"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Window.Resources>
<Storyboard x:Key="Storyboard1" >
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="SomeDouble" Storyboard.TargetName="m">
<EasingDoubleKeyFrame KeyTime="0:0:5.4" Value="100">
<EasingDoubleKeyFrame.EasingFunction>
<CircleEase EasingMode="EaseOut"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
<Grid>
<test:MyUserControl x:Name="m" SomeDouble="5"></test:MyUserControl>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="311,50,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</Window>
后面的按钮代码启动故事板......
该属性在后面的代码中没有改变,同时对其进行动画处理......