我想讓網格(及其上的所有控件)在我的WP7應用程序中搖動。似乎抵消網格的最好方法是使用Margin屬性,但WP7中不支持ThicknessAnimationUsingKeyFrames,所以我不能直接進行。如何將xaml故事板綁定到WP7中的自定義控件的Point屬性?
相反,我創建了一個自定義的控制:
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財產從來沒有被調用。我在「邊距=新厚度(value.X,value.Y」)處設置了一個斷點,但它從來沒有被擊中,我做錯了什麼?
在編輯器中,每當我更改OriginOffset時,邊距也會改變如預期。
任何幫助,非常感謝! 感謝
不OriginOffset需要是一個的DependencyProperty支持數據綁定? –
太棒了!謝謝。我已經過了例外,但沒有任何反應......更新了上面的代碼片段。有什麼想法嗎? – swinefeaster