2011-09-23 63 views
0

我在Silverlight 4中创建了一个自定义绘制的“路径”控件,其中包含一些构成DataTemplate的texblock。我使用Expression Blend中的动画窗口创建了一个简单的动画,可以“播放”并在Blend中看到工作正常。为什么DataTemplate中的故事板不能开始?

我想让这个动画在'_MouseEnter()'事件(VB.NET)上触发我想在动画上发出.Begin方法。看起来很简单。

但是在运行时没有任何反应。我在_MouseEnter事件上放置了一个断点,当鼠标进入控件时它已经进入事件,它运行代码行开始动画,但没有任何反应。没有例外,没有动画,什么都没有。

任何人都可以告诉我我在这里失踪,因为我知道实际的动画确实工作,它只是不运行在运行时? XAML和事件都低于(取消了Texblocks一些样式属性等方面做出更易于阅读):

<DataTemplate x:Key="MyItemTemplate"> 
     <Grid Width="50" Height="80" Opacity="0.9" 
      RenderTransformOrigin="0.5,0.5" 
      ToolTipService.ToolTip="{Binding ItemName}">    

      <Grid.Resources> 
       <Storyboard x:Name="MyItemTemplateAnimate"> 
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[4].(GradientStop.Offset)" 
          Storyboard.TargetName="path"> 
           <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.296"/> 
           <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="0.384"/> 
           <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0.475"/> 
           <EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="0.529"/> 
           <EasingDoubleKeyFrame KeyTime="0:0:0.7" Value="0.587"/> 
           <EasingDoubleKeyFrame KeyTime="0:0:0.8" Value="0.652"/> 
           <EasingDoubleKeyFrame KeyTime="0:0:0.9" Value="0.582"/> 
           <EasingDoubleKeyFrame KeyTime="0:0:1" Value="0.523"/> 
           <EasingDoubleKeyFrame KeyTime="0:0:1.1" Value="0.5"/> 
        /DoubleAnimationUsingKeyFrames> 
       </Storyboard> 
      </Grid.Resources> 

      <Grid.RenderTransform> 
       <CompositeTransform Rotation="180"/> 
      </Grid.RenderTransform> 

      <Path x:Name="path" Data="M 0,0 L 50,0 50,50 25,80 0,50 0,0" Stroke="Wheat" StrokeThickness="2"> 
       <Path.Fill> 
        <LinearGradientBrush EndPoint="-0.419,0.662" MappingMode="RelativeToBoundingBox" StartPoint="1.051,-0.137"> 
         <GradientStop Color="#FF250A0A" Offset="1"/> 
         <GradientStop Color="#FF250A0A"/> 
         <GradientStop Color="#FF501616" Offset="0.725"/> 
         <GradientStop Color="#FF501616" Offset="0.275"/> 
         <GradientStop Color="#FF9F4C4C" Offset="0.5"/> 
        </LinearGradientBrush> 
       </Path.Fill> 
      </Path> 

      <TextBlock x:Name="TextBlock1"        
      </TextBlock> 
      <TextBlock x:Name="TextBlock2" 
      </TextBlock>          
     </Grid> 
    </DataTemplate> 

,它使用的DataTemplate代码:

<m:MapItemsControl x:Name="MyItems" ItemTemplate="{StaticResource MyItemTemplate}"/> 

这里是VB.NET事件:

Private Sub MyItems_MouseEnter(sender As Object, e As System.Windows.Input.MouseEventArgs) Handles MyItems.MouseEnter     

     MyItemTemplateAnimate.Begin() 
End Sub 

回答

0

由于您的故事板位于ItemTemplate中,因此每个项目将会有一个故事板。所以我不确定你为什么没有得到异常,但我认为这是一个名称范围问题。

如果你的动画是在个别项目当鼠标在该项目本身,而不是ItemsControl的为什么不开始呢?

+0

我的控制命名听起来混乱,但他们*不*内“的ItemTemplate '而是一个'DataTemplate'。要回答第二个问题,DataTemplate中的路径对象本身没有任何代码中暴露的事件。只有DataTemplate本身公开像'MouseEnter'这样的事件。该路径不公开这些属性。 – atconway

0

是故事板TargetProperty正确的,因为你正在使用GradientBrush,但在你的路径,你有一个LinearGradientBrush。只是一个疯狂的猜测。 :)

相关问题