将PointAnimation的TargetName设置为资源名称抛出InvalidOperationException(XAML / Silverlight)

时间:2011-01-19 23:51:59

标签: xaml silverlight-4.0 binding resources storyboard

我有一个Button控件,我正在定义一个自定义模板。使用一些Storyboard我可以成功地操纵控件属性,用DoubleAnimation命名变换和效果;使用PointAnimation时出现问题,即按InvalidOperationException投出Cannot resolve TargetName OverlayEllipse.private void Button_MouseEnter(object sender, MouseEventArgs e) { PopUpStoryboard.Begin(); }

动画以代码启动,只需:

<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>

相关的XAML:

Ellipse

显然,Storyboard.Begin可以解析资源,因为它显示正常,并且直接从Storyboard调用抛出异常。我如何更正我拥有的内容,以便StaticResource能够按名称解析为资源?

我曾想过使用Target绑定,但是,考虑到它直接引用对象而不是它的名称,它将不起作用。我只是尝试使用TargetName绑定设置StaticResource而不是The property 'Target' does not exist on the type 'PointAnimation' in the namespace http://schemas.microsoft.com/winfx/2006/xaml/presentation'. - 这会给我一个构建错误(奇怪的是?)说明:{{1}}

感谢。

1 个答案:

答案 0 :(得分:1)

不是定位资源,而是定位控件上的属性(Ellipse)。添加(Shape.Fill)以定位GradientOrigin

<PointAnimation
  Storyboard.TargetName="OverlayEllipse"
  Storyboard.TargetProperty="(Shape.Fill).(RadialGradientBrush.GradientOrigin)"
  Duration="0:0:.1"
  To="0.9,1.2"/>

编辑:这是一个完整的示例按钮样式。

<Style x:Key="ButtonStyle1" TargetType="Button">
  <Setter Property="Background" Value="#FF1F3B53"/>
  <Setter Property="Foreground" Value="#FF000000"/>
  <Setter Property="Padding" Value="3"/>
  <Setter Property="BorderThickness" Value="1"/>
  <Setter Property="BorderBrush">
    <Setter.Value>
      <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="#FFA3AEB9" Offset="0"/>
        <GradientStop Color="#FF8399A9" Offset="0.375"/>
        <GradientStop Color="#FF718597" Offset="0.375"/>
        <GradientStop Color="#FF617584" Offset="1"/>
      </LinearGradientBrush>
    </Setter.Value>
  </Setter>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="Button">
        <Grid>
          <Grid.Resources>
            <RadialGradientBrush x:Key="Brush1" GradientOrigin="0.5,0.5">
                    <GradientStop Color="White" Offset="0"/>
                    <GradientStop Color="#F9FFFFFF" Offset="0.375"/>
                    <GradientStop Color="#E5FFFFFF" Offset="0.625"/>
                    <GradientStop Color="#C6FFFFFF" Offset="1"/>
                  </RadialGradientBrush>
          </Grid.Resources>
          <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CommonStates">
              <VisualState x:Name="Normal"/>
              <VisualState x:Name="MouseOver">
                <Storyboard>
                  <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundAnimation"/>
                  <PointAnimation Duration="0" To="0,0.5" Storyboard.TargetProperty="(Shape.Fill).(RadialGradientBrush.GradientOrigin)" Storyboard.TargetName="BackgroundGradient" d:IsOptimized="True"/>
                </Storyboard>
              </VisualState>
              <VisualState x:Name="Pressed">
                <Storyboard>
                  <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundAnimation"/>
                </Storyboard>
              </VisualState>
              <VisualState x:Name="Disabled">
                <Storyboard>
                  <DoubleAnimation Duration="0" To=".55" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="DisabledVisualElement"/>
                </Storyboard>
              </VisualState>
            </VisualStateGroup>
            <VisualStateGroup x:Name="FocusStates">
              <VisualState x:Name="Focused">
                <Storyboard>
                  <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisualElement"/>
                </Storyboard>
              </VisualState>
              <VisualState x:Name="Unfocused"/>
            </VisualStateGroup>
          </VisualStateManager.VisualStateGroups>
          <Border x:Name="Background" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="3">
            <Grid Background="{TemplateBinding Background}" Margin="1">
              <Border x:Name="BackgroundAnimation" Background="#FF448DCA" Opacity="0"/>
              <Rectangle x:Name="BackgroundGradient" Fill="{StaticResource Brush1}">
              </Rectangle>
            </Grid>
          </Border>
          <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
          <Rectangle x:Name="DisabledVisualElement" Fill="#FFFFFFFF" IsHitTestVisible="false" Opacity="0" RadiusY="3" RadiusX="3"/>
          <Rectangle x:Name="FocusVisualElement" IsHitTestVisible="false" Margin="1" Opacity="0" RadiusY="2" RadiusX="2" Stroke="#FF6DBDD1" StrokeThickness="1"/>
        </Grid>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>