2009-09-18 5 views
1

Storyboard 오브젝트의 인스턴스에 대한 참조가 있으며이 프레임 워크 요소가 첨부되거나/애니메이션으로 연결되는 Framework 요소를 보유하고자합니다. 나는 이것을하는 어떤 방법을 생각해 낼 수 없었다.스토리 보드에서 프레임 워크 요소 가져 오기

예를 들어, 아래의 XAML에서, 나는, 라벨 또는 그리드

나는이 작업을 수행해야하는 이유 지구에 궁금 분들을 위해
<Grid> 
    <Grid.Resources> 
     <Storyboard x:Key="myStoryboard"> 
      <DoubleAnimation Storyboard.TargetProperty="Opacity" From="1" To="0" Duration="0:0:5"/> 
     </Storyboard> 
     <Style x:Key="myStyle" TargetType="{x:Type Label}"> 
      <Style.Triggers> 
       <DataTrigger 
       Binding="{Binding Path=StartAnimation}" Value="true"> 
        <DataTrigger.EnterActions> 
         <BeginStoryboard Storyboard="{StaticResource myStoryboard}" />        
        </DataTrigger.EnterActions> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </Grid.Resources> 
    <Label x:Name="labelHello" Grid.Row="0" Style="{StaticResource myStyle}">Hello</Label> 
</Grid> 

중 하나를 잡아 스토리 보드에 대한 참조로 갈 수있다 그것은 파생 된 Storyboard 클래스 또는 Storyboard가 이벤트가 완료되었을 때 호출 될 DataContext에 메서드 이름을 지정할 수있게 해주는 연결된 동작을 만들기 때문에 발생합니다. 이렇게하면 내 View Model을 호출하기 위해 코드가 필요없는 순수 MVVM을 수행 할 수 있습니다.

답변

1

당신은 이런 식으로 XAML을 변경 한 경우 :

<Grid x:Name="grid"> 
    <Grid.Resources> 
     <Storyboard x:Key="myStoryboard"> 
      <DoubleAnimation Storyboard.TargetProperty="Opacity" From="1" To="0" Duration="0:0:5" Storyboard.Target="{Binding ElementName = grid}"/> 
     </Storyboard> 
     <Style x:Key="myStyle" TargetType="{x:Type Label}"> 
      <Style.Triggers> 
       <DataTrigger 
       Binding="{Binding Path=StartAnimation}" Value="true"> 
        <DataTrigger.EnterActions> 
         <BeginStoryboard Storyboard="{StaticResource myStoryboard}" />        
        </DataTrigger.EnterActions> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </Grid.Resources> 
    <Label x:Name="labelHello" Grid.Row="0" Style="{StaticResource myStyle}">Hello</Label> 
</Grid> 

이 제품은 X를 소개 : 그리드와 DoubleAnimation에 Storyboard.Target에 이름을 지정합니다. 이제 다음 코드로 그리드에 대한 참조를 얻을 수 있습니다 :

Storyboard sb = //You mentioned you had a reference to this. 
var timeLine = sb.Children.First(); 
var myGrid = Storyboard.GetTarget(timeLine); 
+0

감사합니다. 스토리 보드 타겟은 행동을 동일하게 유지하기 위해 labelHello 여야하며 그리드의 이름을 지정하지 않아도됩니다. XAML을 변경하지 않고도 누군가가이 작업을 수행 할 수 있는지 알아볼 수는 있지만 첨부 된 속성을 필요한 유일한 태그로 사용하려면이 항목을 대답으로 표시해야합니다. –

관련 문제