将 PointAnimation 的 TargetName 设置为资源名称会引发 InvalidOperationException (XAML/Silverlight)
我有一个 Button
控件,我正在为其定义自定义模板。使用一些 Storyboard
,我可以使用 DoubleAnimation
成功操作控件属性、命名变换和效果;使用 PointAnimation
时出现了问题,根据 VS,会抛出 InvalidOperationException
:无法解析 TargetName OverlayEllipse。
动画已启动在代码中,简单地说:
private void Button_MouseEnter(object sender, MouseEventArgs e)
{
PopUpStoryboard.Begin();
}
相关的 XAML:
<Button ...>
<Button.Resources>
<RadialGradientBrush
x:Name="OverlayBrush" ...>
<RadialGradientBrush.GradientStops>
...
</RadialGradientBrush.GradientStops>
</RadialGradientBrush>
<Storyboard x:Name="PopUpStoryboard">
...
<PointAnimation
Storyboard.TargetName="OverlayEllipse"
Storyboard.TargetProperty="(Shape.Fill)(RadialGradientBrush.GradientOrigin)"
Duration="0:0:.1"
To="0.9,1.2"/>
</Storyboard>
<Button.Resources>
<Button.Style>
<Style TargetType="Button">
...
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid>
...
<Ellipse x:Name="OverlayEllipse" Fill="{StaticResource OverlayBrush}"/>
...
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Style>
</Button>
Ellipse
显然可以解析资源,因为它显示得很好,并且直接从 Storyboard.Begin
调用抛出异常。如何更正我所拥有的内容,以便 Storyboard
能够按名称解析资源?
我曾想过使用 StaticResource
绑定,但是,考虑到它直接引用对象而不是其名称,这是行不通的。我只是尝试使用 StaticResource
绑定设置 Target
,而不是 TargetName
- 这给了我一个构建错误(很奇怪?)说明:名称空间中的类型“PointAnimation”上不存在属性“Target”http://schemas.microsoft.com/winfx/2006/xaml/presentation'。
谢谢。
I have a Button
control for which I'm defining a custom template. Using some Storyboard
's I can successfully manipulate control properties, named transforms and effects with DoubleAnimation
's; the problem has arisen, when using a PointAnimation
, that an InvalidOperationException
is thrown as, per VS: Cannot resolve TargetName OverlayEllipse.
The animation is initiated in code, simply:
private void Button_MouseEnter(object sender, MouseEventArgs e)
{
PopUpStoryboard.Begin();
}
And the relevant XAML:
<Button ...>
<Button.Resources>
<RadialGradientBrush
x:Name="OverlayBrush" ...>
<RadialGradientBrush.GradientStops>
...
</RadialGradientBrush.GradientStops>
</RadialGradientBrush>
<Storyboard x:Name="PopUpStoryboard">
...
<PointAnimation
Storyboard.TargetName="OverlayEllipse"
Storyboard.TargetProperty="(Shape.Fill)(RadialGradientBrush.GradientOrigin)"
Duration="0:0:.1"
To="0.9,1.2"/>
</Storyboard>
<Button.Resources>
<Button.Style>
<Style TargetType="Button">
...
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid>
...
<Ellipse x:Name="OverlayEllipse" Fill="{StaticResource OverlayBrush}"/>
...
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Style>
</Button>
The Ellipse
can resolve the resource, obviously, as it displays just fine and the exception is thrown directly from the Storyboard.Begin
call. How can I correct what I have in order for the Storyboard
to be able to resolve to resource by name?
I had thought of using StaticResource
binding, however, that won't work considering it refers directly to the object, not its name. I just tried to set the Target
, as opposed to TargetName
, using the StaticResource
binding - this gives me a build error (strangely enough?) stating: The property 'Target' does not exist on the type 'PointAnimation' in the namespace http://schemas.microsoft.com/winfx/2006/xaml/presentation'.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仍然以控件(椭圆)上的属性为目标,而不是以资源为目标。添加
(Shape.Fill)
以定位GradientOrigin
。编辑:这是一个完整的示例按钮样式。
Instead of targeting the resource, still target the property on the control (the Ellipse). Add
(Shape.Fill)
to target theGradientOrigin
.Edit: Here's a complete sample button style.