我有属性(property):
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:
<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 接口(interface),为什么 set 方法不会改变? 换句话说,为什么行 this.Title = _X.ToString() ; 不会同时执行 Storyboard动画。每次 Storyboard 动画时如何更新 X 属性?
编辑
我忘了说我需要这个功能的原因。我经常需要为不在 xaml 中的属性设置动画。例如,假设我想为主音量或鼠标位置设置动画。我知道我可以创建一个线程,然后进行计算并创建我自己的动画。但如果 wpf 已经有几个缓动函数和动画,为什么要重新发明轮子。如果我可以使用 expresion blend 创建一个 Storyboard ,然后使用该 Storyboard 为我的代码中的属性设置动画,那就太好了。
编辑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>
后面的按钮代码启动 Storyboard...
属性在后面的代码中没有改变,同时对其进行动画处理......
请您参考如下方法:
如果我没理解错的话,你要为 TranslateTransform 的 X 属性设置动画,它也绑定(bind)到你的 X 属性(你没有提到定义它的类)。
即使您声明了双向绑定(bind),您的方法也不会奏效。 dependency property例如 TranslateTransform.X 从各种提供者获取其值,例如样式 setter 、触发器、动画、值继承、属性元数据的默认值、本地值等等(其中数据绑定(bind)设置本地值(value))。这些提供程序按特定顺序应用,即使用特定的 precedence。 .
在您的场景中,这意味着 RenderTransform.X 从数据绑定(bind)中获取其本地值。当您启动一个动画时,有效的属性值来自该动画,只要它正在运行或保持该值。同时属性的本地值不会改变,这意味着对绑定(bind)没有影响(即使它是双向的)。
如果您想为您的X 属性设置动画,您可以将其设为custom dependency property。并直接对其进行动画处理。
