2011-03-12 92 views
3

我的问题是,我有一个Button,我想访问Storyboard,这是分配的风格的一部分。访问风格的按钮故事板

<Button x:Name="pictureFolderButton" Content="Pictures" Style="{StaticResource ImageTileButtonStyle}" Click="pictureFolderButton_Click" /> 

风格是非常全面的,所以我会后只有其中的一部分:

<Style x:Key="ImageTileButtonStyle" TargetType="{x:Type Button}"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type Button}"> 
         <ControlTemplate.Resources> 
          <Storyboard x:Key="OnLoaded1"/> 
         </ControlTemplate.Resources> 
         <Grid> 
          <VisualStateManager.VisualStateGroups> 
           <VisualStateGroup x:Name="CommonStates"> 
            <VisualState x:Name="Normal"/> 
            ... 
           </VisualStateGroup> 
           <VisualStateGroup x:Name="AnimationStates"> 
            <VisualStateGroup.Transitions> 
             <VisualTransition GeneratedDuration="0:0:1"> 
              <VisualTransition.GeneratedEasingFunction> 
               <CircleEase EasingMode="EaseOut"/> 
              </VisualTransition.GeneratedEasingFunction> 
             </VisualTransition> 
            </VisualStateGroup.Transitions> 

            <VisualState x:Name="ExpandedFull"> 
             <Storyboard x:Name="expandStoryBoard" > 
              <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)" Storyboard.TargetName="border1"> 

               <EasingDoubleKeyFrame KeyTime="0:0:1" Value="130"/> 
               <EasingDoubleKeyFrame KeyTime="0:0:3" Value="130"/> 
               <EasingDoubleKeyFrame KeyTime="0:0:4" Value="47"/> 
               <EasingDoubleKeyFrame KeyTime="0:0:8" Value="47"/> 
              </DoubleAnimationUsingKeyFrames> 
             </Storyboard> 
            </VisualState> 
           </VisualStateGroup> 
          </VisualStateManager.VisualStateGroups> 

          <ContentPresenter RecognizesAccessKey="True" VerticalAlignment="Stretch" Margin="0,47,0,0" /> 

         </Grid> 

        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 

我只想当“ExpandedFull”动画结束时获取通知。因此,我认为我必须以编程方式获取“expandStoryBoard”,并添加一个完成的事件处理程序。

我得到了工作的唯一的事情是在运行时访问按钮的风格:

Style style = pictureFolderButton.FindResource("ImageTileButtonStyle") as Style; 

如何我一定要继续吗? 非常感谢!

回答

2

只是这种尝试StoryBoard名“OnLoaded1”:

<Button Height="75" Width="120" Style="{StaticResource ImageTileButtonStyle}" Click="Button_Click" >Hello</Button> 


    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     Button btn=(Button)sender; 

     Storyboard stb = btn.TryFindResource("OnLoaded1") as Storyboard; 

    } 
+0

嗯,我想访问内部S “ExpanedFull”状态的Toryboard。 – brilliantvision 2011-03-12 12:09:07

2

从理论上讲,你应该能够往下走你的按钮进入故事板的视觉和逻辑树,但这是相当繁琐如果你的名字在模板中Grid“网格”,像下面威力工作:

Grid grid = pictureFolderButton.FindName("grid") as Grid; 
IList groups = VisualStateManager.GetVisualStateGroups(grid); 
VisualStateGroup targetGroup = null; 
foreach (var group in groups) 
{ 
    if (group is VisualStateGroup && (group as VisualStateGroup).Name == "AnimationStates") 
    { 
     targetGroup = group as VisualStateGroup; 
     break; 
    } 
} 
if (targetGroup != null) 
{ 
    IList states = targetGroup.States; 
    VisualState targetState = null; 
    foreach (var state in states) 
    { 
     if (state is VisualState && (state as VisualState).Name == "ExpandedFull") 
     { 
      targetState = state as VisualState; 
      break; 
     } 
    } 
    if (targetState != null) 
    { 
     targetState.Storyboard.Completed += new EventHandler(Expansion_Completed); 
    } 
    else throw new Exception("VisualState not found."); 
} 
else throw new Exception("VisualStateGroup not found."); 

想到的是提取您的故事板到资源的另一种方式,但我不知道这会不会有什么副作用,即:

<ControlTemplate.Resources> 
    ... 
    <Storyboard x:Key="expandStoryBoard" x:Name="expandStoryBoard"> 
     <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)" Storyboard.TargetName="border1"> 
      <EasingDoubleKeyFrame KeyTime="0:0:1" Value="130"/> 
      <EasingDoubleKeyFrame KeyTime="0:0:3" Value="130"/> 
      <EasingDoubleKeyFrame KeyTime="0:0:4" Value="47"/> 
      <EasingDoubleKeyFrame KeyTime="0:0:8" Value="47"/> 
     </DoubleAnimationUsingKeyFrames> 
    </Storyboard> 
</ControlTemplate.Resources> 
... 
<VisualState x:Name="ExpandedFull" Storyboard="{StaticResource expandStoryBoard}"/> 

那么你应该能够使用FindResource上的按钮来获得故事板。

希望有些作品或至少有一点帮助。

0

如果将Storyboard添加到资源中,可以为XAML文件中的Timeline.Completed事件设置事件处理程序,并在相应的类中实现处理程序。

在你的控制这样的参考资料部分定义Storyboard

<UserControl.Resources> 
    <Storyboard x:Key="expandStoryBoard" Completed="OnExpandCompleted"> ... </Storyboard> 
    ... 
</UserControl.Resources> 

参考的Storyboard为静态资源:

<VisualState x:Name="ExpandedFull" Storyboard="{StaticResource expandStoryBoard}" /> 

在相应的类实现Completed事件处理程序:

void OnExpandCompleted(object sender, EventArgs e) 
{ 
    ... 
}