2011-03-12 93 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) 
{ 
    ... 
}