2010-03-20 6 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)); 
    } 

원래 usercontrols가 콘텐츠를 지원하지 않는다는 것을 알기 전에 나는이 코드를 사용자 정의 컨트롤 대신 usercontrol 내부에서 사용하고있었습니다.

답변

1

한 가지 방법은 해당 OnXxx 메서드를 재정의하는 것입니다. 예를 들어 :

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

당신은 또한 당신의 생성자에서 이벤트 구독을 추가 할 수 있습니다

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

참고 이벤트에 스토리 보드를 실행하기 위해, 당신은 코드 숨김없이이 작업을 수행 할 종종 수 Eventtriggers에를 사용하여 -하지만 IsFadeEnabled 속성을 존중하도록 EventTrigger 기반 접근 방식을 만들 수 있는지 여부는 확실하지 않습니다.

+0

감사합니다. 그러나 " ''Minimalistic_Writer.FadingCanvas '의 이름 범위에서 이름을 찾을 수 없습니다." "fadeInStoryboard.Begin (this);"에서 – Justin

+0

내 생각 엔 Loaded 핸들러에서 this.Name이 null을 리턴한다는 것입니다. TargetName 대신 Target 속성을 설정해보십시오. 'Storyboard.Target = this;'. – itowlson

+0

대단히 고마워요! – Justin

관련 문제