2011-03-12 3 views
3

내 문제는 내가 Button이고 할당 된 스타일의 일부인 Storyboard에 액세스하려고합니다.스타일의 스토리 보드에 액세스하기

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

스타일은 내가의 일부를 게시합니다 그래서 매우 포괄적이다

난 그냥 "ExpandedFull"애니메이션이 종료 될 때 알림을받을하려는
<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> 

. 따라서 프로그래밍 방식으로 "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"상태의 토리 보드. – brilliantvision

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를 정의

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

는 해당 클래스의 Completed 이벤트 처리기를 구현 : 정적 자원으로

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

참조하는 Storyboard

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