1
我有一個Canvas
名爲Marker
的Ellipse
對象:問題將在代碼中使用故事板動畫
<!-- Boilerplate -->
<Window x:Class="WpfApplication6.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfApplication6="clr-namespace:WpfApplication6"
Title="MainWindow" Height="350" Width="525"
SnapsToDevicePixels="True">
<Grid>
<Canvas x:Name="DrawingCanvas">
<!-- ##### Item of interest, below ##### -->
<Ellipse x:Name="Marker" Canvas.Left="200" Canvas.Top="100"
Width="25" Height="25" Stroke="Black" Fill="#E0E000">
<Ellipse.Effect>
<BlurEffect x:Name="MarkerBlurEffect" Radius="0.0" />
</Ellipse.Effect>
</Ellipse>
</Canvas>
</Grid>
</Window>
我試圖以編程方式給Marker
橢圓模糊動畫:
DoubleAnimation blurEffectAnimation=new DoubleAnimation
{
From=0,
To=10,
Duration=TimeSpan.FromSeconds(2.0)
};
// If I uncomment the line below, the blur effect animation works perfectly
//Marker.Effect.BeginAnimation(BlurEffect.RadiusProperty, blurEffectAnimation);
// If I do it using the storyboard code below, the animation has no effect
Storyboard.SetTarget(blurEffectAnimation, Marker.Effect);
Storyboard.SetTargetProperty(blurEffectAnimation,
new PropertyPath(BlurEffect.RadiusProperty));
Storyboard sb=new Storyboard();
sb.Children.Add(blurEffectAnimation);
sb.Begin();
有趣的是,如果我通過名稱註冊效果並在故事板中使用該名稱,效果將再次開始工作:
// Register the MarketBlurEffect (as it's named in the XAML) effect by name
// with the FrameworkElement being animated (why do I need to?)
Marker.RegisterName("MarkerBlurEffect",MarkerBlurEffect);
// Instead of SetTarget, use SetTargetName on the registered name
Storyboard.SetTargetName(blurEffectAnimation, "MarkerBlurEffect");
Storyboard.SetTargetProperty(blurEffectAnimation,
new PropertyPath(BlurEffect.RadiusProperty));
Storyboard sb=new Storyboard();
sb.Children.Add(blurEffectAnimation);
// Provide Marker to the Begin function in order to specify the name scope
// for the registered ("MarkerBlurEffect") name. Not doing this will result
// in an InvalidOperationException with the Message property set to:
// No applicable name scope exists to resolve the name 'MarkerBlurEffect'.
sb.Begin(Marker);
嗯,這很有趣,你設定的目標是橢圓形(標記),而不是橢圓形的效果]屬性。然後,通過更復雜的路徑引用該屬性,該路徑提及了形狀的「效果」屬性及其「半徑」屬性。你的方法當然確實有效,所以+1現在。這使得看起來Storyboard的SetTarget函數需要一個FrameworkElement而不僅僅是一個依賴屬性,儘管SetTarget函數的原型導致人們不相信。 – 2012-01-13 16:22:55
我剛剛在路徑中用EllipseGeometry的RadiusX屬性嘗試了同樣的事情。不適用於目標Path.Data和屬性RadiusX,但適用於目標Path和屬性(Data).RadiusX。我同意對期望FrameworkElement或至少是UIElement的SetTarget的解釋。我認爲這與樹或樹的深度無關。 – Clemens 2012-01-13 16:39:34
更多信息是[這裏](http://stackoverflow.com/questions/2957582/wpf-storyboard-settarget-vs-storyboard-settargetname)和[這裏](http://stackoverflow.com/questions/2338714/why -dont-these-animations-work-when-im-using-a-storyboard) – Clemens 2012-01-13 16:47:38