2014-07-22 1 views
0

테이블 셀과 같은 격자 모양으로 배열 된 사각형의 애니메이션을 사용하는 코드로 작업하고 있습니다. 애니메이션은 지금 mouseover 이벤트에서 트리거되지만 대신 자신의 사용자 정의 메소드 호출로 대체해야합니다. 터치리스 커서 (kinect 사용)로 작업하고 기본적으로 커서가 현재 마우스/마우스 오버 속성과 비슷한 특정 셀/사각형에 있는지를 확인하는 자체 메서드가 기본적으로 있기 때문에 필요합니다. 다음은 현재 사용중인 애니메이션 스타일 코드입니다.WPF에서 사용자 정의 메서드 호출로 마우스 이벤트 호출을 바꾸는 방법 애니메이션 스타일

 Style PrepareAnimationStyle(String label) 
     { 
      Trigger animTrigger = new Trigger(); 
      animTrigger.Property = ContentElement.IsMouseOverProperty; //currently set on mouse hover. Will have to call explicitly for cursor. 
      animTrigger.Value = true; 

      System.Windows.Media.Animation.ColorAnimation greenStroke = new System.Windows.Media.Animation.ColorAnimation((Color)ColorConverter.ConvertFromString("#FF66CC00"), TimeSpan.FromSeconds(0)); 
      //greenStroke.FillBehavior = FillBehavior.HoldEnd; 
      System.Windows.Media.Animation.ColorAnimation greenFill = new System.Windows.Media.Animation.ColorAnimation((Color)ColorConverter.ConvertFromString("#FF66CC00"), TimeSpan.FromSeconds(0)); 
      //greenFill.FillBehavior = FillBehavior.HoldEnd; 
      System.Windows.Media.Animation.ColorAnimation transparentFill = new System.Windows.Media.Animation.ColorAnimation(Colors.Transparent, TimeSpan.FromSeconds(1)); 
      System.Windows.Media.Animation.ColorAnimation silverStroke = new System.Windows.Media.Animation.ColorAnimation(Colors.Silver, TimeSpan.FromSeconds(1)); 

      System.Windows.Media.Animation.Storyboard sbEnter = new System.Windows.Media.Animation.Storyboard(); 
      Storyboard.SetTargetProperty(greenStroke, new PropertyPath("Stroke.Color")); 
      Storyboard.SetTargetProperty(greenFill, new PropertyPath("Fill.Color")); 
      sbEnter.Children.Add(greenStroke); 
      sbEnter.Children.Add(greenFill); 

      Storyboard sbExit = new Storyboard(); 
      Storyboard.SetTargetProperty(silverStroke, new PropertyPath("Stroke.Color")); 
      Storyboard.SetTargetProperty(transparentFill, new PropertyPath("Fill.Color")); 
      sbExit.Children.Add(silverStroke); 
      sbExit.Children.Add(transparentFill); 

      animTrigger.EnterActions.Add(new BeginStoryboard() { Storyboard = sbEnter }); 
      if (label != "chills") 
       animTrigger.ExitActions.Add(new BeginStoryboard() { Storyboard = sbExit }); 

      Style cellStyle = new Style(); 
      cellStyle.Triggers.Add(animTrigger); 

      return cellStyle; 
     } 

이 줄을 바꿀 수 있습니까? :

animTrigger.Property = ContentElement.IsMouseOverProperty; 

이 작업을 수행 할 수 있습니까? 그렇다면 어떻게 자체 메서드 호출로 바꿀 수 있습니까?

대신 특정 UI 요소에 액세스하여 명시 적으로 mouseover 속성을 True로 설정할 수 있습니까? 다음과 같은 것 :

 foreach (UIElement ui in grid.Children) 
    { 
       int index = grid.Children.IndexOf(ui); 
       int rowIndex = index/numOfCols; 
       int columnIndex = index % numOfCols; 

       if (rowIndex == 0 && columnIndex == 5) 
        if (ui is System.Windows.Shapes.Rectangle) 
         ui.SetValue(UIElement.IsMouseOverProperty, true); 
    } 

두 번째 방법이 효과가 있습니까? 속성을 false로 설정해야합니까?

도와주세요!

답변

1

당신은 당신의 UI 요소에 깃발을 너무 첨부 된 종속성 속성을 가진 클래스를 정의 그런 행동

을 주입 Attached Properties의 사용을 확인하고 설정하는 데 사용 및 해제 할 수 있습니다

AnimationTrigger 클래스

PrepareAnimationStyle 방법에서
public class AnimationTrigger : DependencyObject 
{ 
    // Using a DependencyProperty as the backing store for IsTriggered. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty IsTriggeredProperty = 
     DependencyProperty.RegisterAttached("IsTriggered", typeof(bool), typeof(AnimationTrigger), new PropertyMetadata(false)); 
} 

변경

animTrigger.Property = AnimationTrigger.IsTriggeredProperty; 
,

마침내

ui.SetValue(AnimationTrigger.IsTriggeredProperty, true); 

애니메이션을 트리거하고 그것이 그대로 당신이 IsMouseOverProperty의 값을 설정하지 못할 수 있습니다 둘째

ui.SetValue(AnimationTrigger.IsTriggeredProperty, false); 

other answers

애니메이션 다시 되돌릴 읽기 전용 속성

또한 값을 true로 설정하고 false로 설정해야하는 애니메이션 사이클을 완료하려면

관련 문제