2014-09-03 3 views
0

InkCanvas에 수많은 Image 컨트롤을 배치해야합니다. 특정 코드 조건이 충족되면 모두 애니메이션 작업을 수행해야합니다. 수많은 시도 후에도 ControlTemplate은 여전히 ​​올바르지 않습니다. 시각적 상태 관리자와 스토리 보드를 사용하여 컨트롤 템플릿에 이미지를 배치하는 방법은 무엇입니까?

 <Image Name="image1" 
       Template="{StaticResource AnimatedImage}" 
       Source="{Binding Path=Image1}" 
       Visibility="{Binding ElementName=chkShowPerson, Path=IsChecked, Converter={StaticResource b2v}}" 
       HorizontalAlignment="Center" 
       VerticalAlignment="Center" > 
     </Image> 
    </InkCanvas> 
</Grid> 

답변

0

VisualStateManager 요소는 컨트롤 템플릿의 루트의 자식으로 위치해야합니다 : 여기

<ControlTemplate x:Key="AnimatedImage" TargetType="{x:Type Image}"> 
    <Grid> 
     <Image Name="image" 
       Source="{TemplateBinding Filename}" 
       Width="{TemplateBinding Width}" 
       Height="{TemplateBinding Height}" 
       HorizontalAlignment="Center" 
       VerticalAlignment="Center"> 

      <VisualStateManager.VisualStateGroups> 
       <VisualStateGroup Name="AnimationLocations"> 

        <!-- When the image is first loaded move it to the center of the InkCanvas --> 
        <VisualState Name="AnimateToCenterScreen"> 
         <Storyboard> 
          <DoubleAnimation Storyboard.TargetName="image" 
              Storyboard.TargetProperty="(Image.RenderTransform).(TranslateTransform.X)" 
              From="{TemplateBinding From}" 
              To="{TemplateBinding To}" 
              Duration="{TemplateBinding Duration}" /> 
         </Storyboard> 
        </VisualState> 

        <!-- Animate the image to an off screen position to effectively hide it. --> 
        <VisualState Name="AnimateToOffScreen"> 
         <!-- To be filled in later. --> 
        </VisualState> 

        <!-- Animate the image back to the original on screen position. --> 
        <VisualState Name="AnimateToOnScreen"> 
         <!-- To be filled in later. --> 
        </VisualState> 
       </VisualStateGroup> 
      </VisualStateManager.VisualStateGroups> 
     </Image> 
    </Grid> 
</ControlTemplate> 

메인 그리드에 대한 XAML입니다.
<ControlTemplate x:Key="AnimatedImage" TargetType="{x:Type Image}"> 
    <Grid> 
     <VisualStateManager.VisualStateGroups> 
      ... 
     </VisualStateManager.VisualStateGroups> 

     <Image Name="image" ... /> 
    </Grid> 
</ControlTemplate> 

(참조 here를 들어 당신이 그 재미있는 이야기를 찾을 수 있습니다 MSDN의 페이지입니다.)

+0

이렇게 빨리 응답 해 주셔서 감사합니다. – user2986938

+0

너무 빨리 응답 해 주셔서 감사합니다. 제안 된 변경 사항을 적용한 후에도 Image 컨트롤에서 AnimatedImage를 템플릿으로 사용할 수 없습니다. Image 컨트롤에는 Template 속성이 없습니다. 너무 많은 문제가 아니라면 xtype Image가있는 controltemplate의 예와 이미지 컨트롤에서 해당 템플릿을 참조하는 방법을 요청할 수 있습니까? – user2986938

+0

@ user2986938 아 죄송합니다. 애니메이션 속성에 TemplateBinding을 사용하려고했는지 알지 못했습니다. 'To','From' 및'Duration' 속성은 종속 속성이 아니므로 바인드 할 수 없습니다. 다른 방법을 찾아야합니다. – McGarnagle