Windows 8 如何使用情节提要设置用户控件依赖项属性的动画?
我试图根据情节提要滚动我的用户控件(其中包含列表视图)。但首先,我需要能够使用故事板为我的自定义属性设置动画 我可以设置自定义属性ScrollPos的动画,但我的set()函数根本不会触发 如何检测到它已更改,以便执行实际的滚动 我的用户控件:Windows 8 如何使用情节提要设置用户控件依赖项属性的动画?,windows-8,windows-runtime,dependency-properties,Windows 8,Windows Runtime,Dependency Properties,我试图根据情节提要滚动我的用户控件(其中包含列表视图)。但首先,我需要能够使用故事板为我的自定义属性设置动画 我可以设置自定义属性ScrollPos的动画,但我的set()函数根本不会触发 如何检测到它已更改,以便执行实际的滚动 我的用户控件: public sealed partial class MyCellListView : UserControl { public MyCellListView() { this.I
public sealed partial class MyCellListView : UserControl
{
public MyCellListView()
{
this.InitializeComponent();
this.DataContext = this;
}
public const string ScrollPosPropertyTag = "ScrollPos";
public static readonly DependencyProperty ScrollPosProperty =
DependencyProperty.Register(
ScrollPosPropertyTag,
typeof(double),
typeof(MyCellListView),
new PropertyMetadata((double)0));
public double ScrollPos
{
get
{
return (double)GetValue(ScrollPosProperty);
}
set
{
// If I set a breakpoint here, it only gets hit on creation to set
// the initial value of ScrollPos --- not during the storyboard.
SetValue(ScrollPosProperty, value);
}
}
}
它所在的页面:
public MyPage // contains MyCellListView
{
private void startScrolling()
{
// Calculate target offset
double targetOffset = m_teacher.getNumUniqueCues();
// Create animation and storyboard
DoubleAnimation animation = new DoubleAnimation();
ExponentialEase ee = new ExponentialEase();
ee.EasingMode = EasingMode.EaseIn;
ee.Exponent = 3;
animation.EasingFunction = ee;
animation.Duration = new Duration(new TimeSpan(0, 0, 30));
animation.From = 0;
animation.To = targetOffset;
Storyboard.SetTarget(animation, m_listView);
Storyboard.SetTargetProperty(animation,
// Works fine here for "(MyCellListView.Opacity)");
"(MyCellListView.ScrollPos)");
Storyboard storyboard = new Storyboard();
storyboard.Children.Add(animation);
// Need this or the dependent property won't get animated
animation.EnableDependentAnimation = true;
storyboard.Begin();
}
}
答案很简单。如文件所述:
public const string ScrollPosPropertyTag = "ScrollPos";
public static readonly DependencyProperty ScrollPosProperty =
DependencyProperty.Register(
ScrollPosPropertyTag,
typeof(double),
typeof(MyCellListView),
new PropertyMetadata((double)0, new PropertyChangedCallback(OnScrollPosChanged)));
public double ScrollPos
{
get
{
return (double)GetValue(ScrollPosProperty);
}
set
{
SetValue(ScrollPosProperty, value);
}
}
private static void OnScrollPosChanged(DependencyObject dob,
DependencyPropertyChangedEventArgs args)
{
if (dob != null)
{
MyCellListView listView = dob as MyCellListView;
listView.DoStuffHere();
}
}