如何在自定义控件中处理事件?

时间:2010-03-20 22:42:34

标签: c# wpf user-controls custom-controls

我不打算创建新活动。我需要创建一个画布控件,可以选择淡入或淡出,具体取决于鼠标是否在它上面。下面的代码可能解释了我想做的比我更好的事情。

        private Storyboard fadeInStoryboard;
    private Storyboard fadeOutStoryboard;
    public FadingOptionPanel()
    {
        InitializeComponent();
    }
    public static readonly DependencyProperty FadeEnabledProperty =
    DependencyProperty.Register("IsFadeEnabled", typeof(bool), typeof(FadingOptionPanel), new FrameworkPropertyMetadata(true,
                   OnFadeEnabledPropertyChanged,
                   OnCoerceFadeEnabledProperty));
    public bool IsFadeEnabled
    {
        get
        {
            return (bool)GetValue(FadeEnabledProperty);
        }
        set
        {
            SetValue(FadeEnabledProperty, value);
        }
    }
    private static void OnFadeEnabledPropertyChanged(DependencyObject source,
    DependencyPropertyChangedEventArgs e)
    {

    }
    private static object OnCoerceFadeEnabledProperty(DependencyObject sender, object data)
    {
        if (data.GetType() != typeof(bool))
        {
            data = true;
        }
        return data;
    }

    private void FadingOptionPanel_MouseEnter(object sender, MouseEventArgs e)
    {
        if (IsFadeEnabled)
        {
            fadeInStoryboard.Begin(this);
        }
    }
    private void FadingOptionPanel_MouseLeave(object sender, MouseEventArgs e)
    {
        if (IsFadeEnabled)
        {
            fadeOutStoryboard.Begin(this);
        }
    }
    private void FadingOptionsPanel_Loaded(object sender, RoutedEventArgs e)
    {
        //Initialize Fade In Animation
        DoubleAnimation fadeInDoubleAnimation = new DoubleAnimation();
        fadeInDoubleAnimation.From = 0;
        fadeInDoubleAnimation.To = 1;
        fadeInDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(.5));
        fadeInStoryboard = new Storyboard();
        fadeInStoryboard.Children.Add(fadeInDoubleAnimation);
        Storyboard.SetTargetName(fadeInDoubleAnimation, this.Name);
        Storyboard.SetTargetProperty(fadeInDoubleAnimation, new PropertyPath(Canvas.OpacityProperty));

        //Initialize Fade Out Animation
        DoubleAnimation fadeOutDoubleAnimation = new DoubleAnimation();
        fadeOutDoubleAnimation.From = 1;
        fadeOutDoubleAnimation.To = 0;
        fadeOutDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(.2));
        fadeOutStoryboard = new Storyboard();
        fadeOutStoryboard.Children.Add(fadeOutDoubleAnimation);
        Storyboard.SetTargetName(fadeOutDoubleAnimation, this.Name);
        Storyboard.SetTargetProperty(fadeOutDoubleAnimation, new PropertyPath(Canvas.OpacityProperty));
    }

我最初在usercontrol中使用此代码而不是自定义控件,然后才发现usercontrols不支持内容。

1 个答案:

答案 0 :(得分:1)

一种方法是覆盖相应的OnXxx方法。例如:

protected override void OnMouseEnter(MouseEventArgs e)
{
  base.OnMouseEnter(e);
  if (IsFadeEnabled)
  {
    fadeInStoryboard.Begin(this);
  }
}

您还可以在构造函数中添加事件订阅:

public FadingOptionPanel()
{
  this.MouseEnter += FadingOptionPanel_MouseEnter;
}

请注意,要在事件上运行故事板,您通常可以使用EventTriggers执行此操作而无需代码隐藏 - 但我不确定是否可以使用基于EventTrigger的方法来尊重您的IsFadeEnabled属性。 / p>