我试图让我的WP7应用程序中的Grid(及其上的所有控件)动摇。似乎偏移网格的最佳方法是使用Margin属性,但WP7中不支持ThicknessAnimationUsingKeyFrames,因此我无法直接执行。
相反,我创建了一个自定义控件:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
namespace MyNamespace
{
// Grid which has an OriginOffset property that is tied to the margin,
// and thus can be animated.
public class OffsettableGrid : Grid
{
public OffsettableGrid()
{
}
public static readonly DependencyProperty OriginOffsetProperty =
DependencyProperty.Register(
"OriginOffset", typeof(Point), typeof(OffsettableGrid), null);
public Point OriginOffset
{
get
{
return new Point(Margin.Left, Margin.Top);
}
set
{
Margin = new Thickness(value.X, value.Y,
Margin.Right, Margin.Bottom);
}
}
}
}
然后在xaml中我声明了一个故事板如下:
<Storyboard x:Name="m_shakeStoryboard"
RepeatBehavior="5x"
AutoReverse="False"
SpeedRatio="5">
<PointAnimationUsingKeyFrames
BeginTime="00:00:00"
Storyboard.TargetName="m_chooseAnswerGrid"
Storyboard.TargetProperty="(OffsettableGrid.OriginOffset)">
<EasingPointKeyFrame KeyTime="00:00:00" Value="0,0"/>
<EasingPointKeyFrame KeyTime="00:00:00.2" Value="-10,0"/>
<EasingPointKeyFrame KeyTime="00:00:00.4" Value="10,0"/>
<EasingPointKeyFrame KeyTime="00:00:00.6" Value="0,0"/>
</PointAnimationUsingKeyFrames>
</Storyboard>
然后我当然使用。
声明我的网格但是当我开始编写故事板时:
m_shakeStoryboard.Begin();
我的OriginOffset属性永远不会被调用。我在“Margin = new Thickness(value.X,value.Y”)中设置了一个断点,但它永远不会被击中。我做错了什么?
在编辑器中,每当我更改OriginOffset时,边距都会随预期而变化。
任何帮助非常感谢! 感谢
答案 0 :(得分:2)
您可能想要检查this。您不必使用EventTrigger或其他东西来启动StoryBoard吗?我认为你不需要括号Storyboard.TargetProperty="(OffsettableGrid.OriginOffset)"
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetName="myAnimatedBrush"
Storyboard.TargetProperty="Color"
From="Red" To="Blue" Duration="0:0:7" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
答案 1 :(得分:1)
您的依赖项属性定义不明确。您应该使用SetValue和GetValue:
public Point OriginOffset
{
get
{
return (Point)GetValue(OriginOffsetProperty);
}
set
{
SetValue(OriginOffsetProperty, value);
}
}
然后向依赖项属性的PropertyMetadata添加一个回调,以设置边距:
public static readonly DependencyProperty OriginOffsetProperty =
DependencyProperty.Register(
"OriginOffset",
typeof(Point),
typeof(OffsettableGrid),
new PropertyMetadata(new PropertyChangedCallback(OnOriginOffsetChanged)));