如何在WPF动画样式中使用自定义方法调用替换鼠标事件调用



我正在使用一个代码,在矩形上使用动画,排列在网格中,如表格单元格。动画触发鼠标悬停事件,但现在需要替换为自己的自定义方法调用。我需要这个,因为我使用非接触式光标(使用kinect),基本上我有自己的方法来检测光标当前是否在特定的单元格/矩形上(很像鼠标悬停属性)。下面是我现在使用的动画样式的代码:

        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吗?

请帮忙!

你可以使用附加属性来注入这样的行为

所以定义一个带有附属依赖属性的类,并使用它来设置和取消UI元素

上的标志。

AnimationTrigger类

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));
}

PrepareAnimationStyle方法的变化

animTrigger.Property = AnimationTrigger.IsTriggeredProperty;

最后触发动画

ui.SetValue(AnimationTrigger.IsTriggeredProperty, true);

和恢复动画

ui.SetValue(AnimationTrigger.IsTriggeredProperty, false);

other answers

其次,你可能无法设置IsMouseOverProperty的值,因为它是一个只读属性

也要完成动画循环,你需要将值设置为true,然后是false

相关内容

  • 没有找到相关文章

最新更新