2014-07-21 3 views
1

WPF의 애니메이션 시퀀스에서 여러 효과를 추가하는 데 문제가 있습니다. 여러 개의 사각형이 격자 및 애니메이션 효과가 작동하는 방식 내에 배치 한 다음과 같은 순서입니다 :WPF를 사용하여 스토리 보드를 사용하여 애니메이션에 여러 효과 추가

기본적으로
  1. , 사용자는 모든 세포가 검은 색에 은색 테두리로 경계되도록 그리드를보고 각 셀 내의 직사각형의 색상은 투명/검정색 일 수 있습니다.
  2. 마우스를 올리면 셀의 사각형이 획을 변경하고 녹색으로 채 웁니다.
  3. 마우스를 끝내면 마우스를 가리 키기 전에 이전 셀의 색이 천천히 기본 상태로 변경됩니다.

획 색상에 대해서만 애니메이션 효과를 적용 할 수 있었지만 채우기 속성과 결합 할 수는 없었습니다. 여기 그리드 내에서 사각형을 만들기위한 코드입니다 : 여기

  Style cellStyle = PrepareAnimationStyle(); 
      foreach (string label in rowHeaders) 
      { 
       for (int n = 0; n < numOfCols; n++) 
        grid.Children.Add(new Rectangle() 
        { 
         Stroke = Brushes.Silver, 
         StrokeThickness = 2, 
         Fill = Brushes.Transparent, 
         Height = cellSize, 
         Width = cellSize, 
         Style = cellstyle 
        }); 
      } 

그리고 애니메이션을 설정하는 코드입니다 (필요에 따라 계속 진행, 작동 할 수 없습니다) :

Style PrepareAnimationStyle() 
{ 
    Trigger animTrigger = new Trigger(); 
    animTrigger.Property = ContentElement.IsMouseOverProperty; 
    animTrigger.Value = true; 

    System.Windows.Media.Animation.ColorAnimation toGreen = new   System.Windows.Media.Animation.ColorAnimation((Color)ColorConverter.ConvertFromString("#FF66CC00"), TimeSpan.FromSeconds(0)); 
    toGreen.FillBehavior = FillBehavior.HoldEnd; 
    System.Windows.Media.Animation.ColorAnimation toTransparent = new System.Windows.Media.Animation.ColorAnimation(Colors.Transparent, TimeSpan.FromSeconds(1)); 
    System.Windows.Media.Animation.ColorAnimation toSilver = 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(toGreen, new PropertyPath("Stroke.Color")); 
    Storyboard.SetTargetProperty(toGreen, new PropertyPath("(Shape.Fill).(SolidColorBrush.Color)")); 
    sbEnter.Children.Add(toGreen); 

    /*Storyboard sbFill = new Storyboard(); 
    Storyboard.SetTargetProperty(toGreen, new PropertyPath("Fill.Color")); 
    sbFill.Children.Add(toSilver); 

    Storyboard sbFillEmpty = new Storyboard(); 
    Storyboard.SetTargetProperty(toTransparent, new PropertyPath("Fill.Color")); 
    sbFillEmpty.Children.Add(toSilver);*/ 

    Storyboard sbExit = new Storyboard(); 
    //Storyboard.SetTargetProperty(toSilver, new PropertyPath("Stroke.Color")); 
    Storyboard.SetTargetProperty(toTransparent, new PropertyPath("Fill.Color")); 
    sbExit.Children.Add(toSilver); 

    animTrigger.EnterActions.Add(new BeginStoryboard() { Storyboard = sbEnter }); 
    //animTrigger.EnterActions.Add(new BeginStoryboard() { Storyboard = sbFill }); 
    //animTrigger.EnterActions.Add(new BeginStoryboard() { Storyboard = sbFillEmpty }); 
    animTrigger.ExitActions.Add(new BeginStoryboard() { Storyboard = sbExit }); 

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

    return cellStyle; 
} 

답변

2
여기

당신은 제대로 작동하면 셀을 추가하는 동안 채우기 및 스트로크를 설정해야합니다 수 있도록하기 위해

Style PrepareAnimationStyle() 
    { 
     Trigger animTrigger = new Trigger(); 
     animTrigger.Property = FrameworkElement.IsMouseOverProperty; 
     animTrigger.Value = true; 

     ColorAnimation strokeToGreen = new ColorAnimation((Color)ColorConverter.ConvertFromString("#FF66CC00"), TimeSpan.FromSeconds(0)); 
     ColorAnimation strokeToSilver = new ColorAnimation(Colors.Silver, TimeSpan.FromSeconds(1)); 

     ColorAnimation fillToGreen = new ColorAnimation((Color)ColorConverter.ConvertFromString("#FF66CC00"), TimeSpan.FromSeconds(0)); 
     ColorAnimation fillToTransparent = new ColorAnimation(Colors.Transparent, TimeSpan.FromSeconds(1)); 

     Storyboard sbEnter = new Storyboard(); 
     Storyboard.SetTargetProperty(strokeToGreen, new PropertyPath("Stroke.Color")); 
     Storyboard.SetTargetProperty(fillToGreen, new PropertyPath("Fill.Color")); 
     sbEnter.Children.Add(strokeToGreen); 
     sbEnter.Children.Add(fillToGreen); 

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

     animTrigger.EnterActions.Add(new BeginStoryboard() { Storyboard = sbEnter }); 
     animTrigger.ExitActions.Add(new BeginStoryboard() { Storyboard = sbExit }); 

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

     return cellStyle; 
    } 

 Style cellStyle = PrepareAnimationStyle(); //this line is out side of the cell loop 
     .... 
     rect.Fill = new SolidColorBrush(Colors.Transparent); 
     rect.Stroke = new SolidColorBrush(Colors.Silver); 
     rect.Style = cellStyle; 
관련 문제