2010-03-20 49 views
0

我不想创建新的事件。我需要创建一个可选择淡入或淡出的画布控件,具体取决于鼠标是否在其上。下面的代码可能解释了我想要做的比我能做得更好。如何处理我的自定义控件中的事件?

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

我原本是使用用户控件,而不是一个自定义的控件中此代码之前,我发现用户控件不支持的内容。

回答

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属性。

+0

谢谢。但是,我收到一个错误“'''名称不能在'Minimalistic_Writer.FadingCanvas'的名称范围中找到。”在“fadeInStoryboard.Begin(this);” – Justin 2010-03-20 23:43:58

+0

我的猜测是,在你的Loaded处理程序中,this.Name返回null。尝试设置Target属性而不是TargetName,例如'Storyboard.Target = this;'。 – itowlson 2010-03-21 00:26:43

+0

非常感谢! – Justin 2010-03-21 00:49:56

相关问题