using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Threading;
namespace TestProject;
/// <summary>
/// 알림 컨트롤
/// </summary>
public partial class NotificationControl : UserControl
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Event
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 닫은 경우 - Closed
/// <summary>
/// 닫은 경우
/// </summary>
public event EventHandler Closed;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Dependency Property
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Public
#region 테두리 두께 속성 - BorderThicknessProperty
/// <summary>
/// 테두리 두께 속성
/// </summary>
public static new readonly DependencyProperty BorderThicknessProperty = DependencyProperty.Register
(
"BorderThickness",
typeof(Thickness),
typeof(NotificationControl),
new PropertyMetadata(new Thickness(0), BorderThicknessPropertyChangedCallback)
);
#endregion
#region 테두리 브러시 속성 - BorderBrushProperty
/// <summary>
/// 테두리 브러시 속성
/// </summary>
public static new readonly DependencyProperty BorderBrushProperty = DependencyProperty.Register
(
"BorderBrush",
typeof(Brush),
typeof(NotificationControl),
new PropertyMetadata(Brushes.Blue, BorderBrushPropertyChangedCallback)
);
#endregion
#region 배경 브러시 - BackgroundProperty
/// <summary>
/// 배경 브러시
/// </summary>
public static new readonly DependencyProperty BackgroundProperty = DependencyProperty.Register
(
"Background",
typeof(Brush),
typeof(NotificationControl),
new PropertyMetadata(Brushes.LightBlue, BackgroundPropertyChangedCallback)
);
#endregion
#region 메시지 속성 - MessageProperty
/// <summary>
/// 메시지 속성
/// </summary>
public static readonly DependencyProperty MessageProperty = DependencyProperty.Register
(
"Message",
typeof(string),
typeof(NotificationControl),
new PropertyMetadata(string.Empty, MessagePropertyChangedCallback)
);
#endregion
#region 표시 시간 속성 - DisplayTimeProperty
/// <summary>
/// 표시 시간 속성
/// </summary>
public static readonly DependencyProperty DisplayTimeProperty = DependencyProperty.Register
(
"DisplayTime",
typeof(TimeSpan),
typeof(NotificationControl),
new PropertyMetadata(TimeSpan.FromSeconds(5))
);
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 테두리 두께 - BorderThickness
/// <summary>
/// 테두리 두께
/// </summary>
public new Thickness BorderThickness
{
get
{
return (Thickness)GetValue(BorderThicknessProperty);
}
set
{
SetValue(BorderThicknessProperty, value);
}
}
#endregion
#region 테두리 브러시 - BorderBrush
/// <summary>
/// 테두리 브러시
/// </summary>
public new Brush BorderBrush
{
get
{
return (Brush)GetValue(BorderBrushProperty);
}
set
{
SetValue(BorderBrushProperty, value);
}
}
#endregion
#region 배경 브러시 - Background
/// <summary>
/// 배경 브러시
/// </summary>
public new Brush Background
{
get
{
return (Brush)GetValue(BackgroundProperty);
}
set
{
SetValue(BackgroundProperty, value);
}
}
#endregion
#region 메시지 - Message
/// <summary>
/// 메시지
/// </summary>
public string Message
{
get
{
return (string)GetValue(MessageProperty);
}
set
{
SetValue(MessageProperty, value);
}
}
#endregion
#region 표시 시간 - DisplayTime
/// <summary>
/// 표시 시간
/// </summary>
public TimeSpan DisplayTime
{
get
{
return (TimeSpan)GetValue(DisplayTimeProperty);
}
set
{
SetValue(DisplayTimeProperty, value);
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - NotificationControl()
/// <summary>
/// 생성자
/// </summary>
public NotificationControl()
{
InitializeComponent();
Loaded += UserControl_Loaded;
this.closeButton.Click += closeButton_Click;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Private
#region 테두리 두께 속성 변경시 콜백 처리하기 - BorderThicknessPropertyChangedCallback(d, e)
/// <summary>
/// 테두리 두께 속성 변경시 콜백 처리하기
/// </summary>
/// <param name="d">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private static void BorderThicknessPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
NotificationControl control = (NotificationControl)d;
control.border.BorderThickness = (Thickness)e.NewValue;
}
#endregion
#region 테두리 브러시 속성 변경시 콜백 처리하기 - BorderBrushPropertyChangedCallback(d, e)
/// <summary>
/// 테두리 브러시 속성 변경시 콜백 처리하기
/// </summary>
/// <param name="d">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private static void BorderBrushPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
NotificationControl control = (NotificationControl)d;
control.border.BorderBrush = (Brush)e.NewValue;
}
#endregion
#region 배경 브러시 속성 변경시 콜백 처리하기 - BackgroundPropertyChangedCallback(d, e)
/// <summary>
/// 배경 브러시 속성 변경시 콜백 처리하기
/// </summary>
/// <param name="d">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private static void BackgroundPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
NotificationControl control = (NotificationControl)d;
control.border.Background = (Brush)e.NewValue;
}
#endregion
#region 메시지 속성 변경시 콜백 처리하기 - MessagePropertyChangedCallback(d, e)
/// <summary>
/// 메시지 속성 변경시 콜백 처리하기
/// </summary>
/// <param name="d">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private static void MessagePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
NotificationControl control = (NotificationControl)d;
control.textBlock.Text = (string)e.NewValue;
}
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Instance
//////////////////////////////////////////////////////////////////////////////// Private
////////////////////////////////////////////////////////////////////// Event
#region 사용자 컨트롤 로드시 처리하기 - UserControl_Loaded(sender, e)
/// <summary>
/// 사용자 컨트롤 로드시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
StartCloseTimer();
}
#endregion
#region 닫기 버튼 클릭시 처리하기 - closeButton_Click(sender, e)
/// <summary>
/// 닫기 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void closeButton_Click(object sender, RoutedEventArgs e)
{
Close();
}
#endregion
#region 페이드 아웃 애니메이션 완료시 처리하기 - fadeOutAnimation_Completed(sender, e)
/// <summary>
/// 페이드 아웃 애니메이션 완료시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void fadeOutAnimation_Completed(object sender, EventArgs e)
{
Panel parentPanel = Parent as Panel;
parentPanel?.Children.Remove(this);
Closed?.Invoke(this, EventArgs.Empty);
}
#endregion
////////////////////////////////////////////////////////////////////// Function
#region 닫기 - Close()
/// <summary>
/// 닫기
/// </summary>
private void Close()
{
DoubleAnimation fadeOutAnimation = new DoubleAnimation
{
From = 1,
To = 0,
Duration = new Duration(TimeSpan.FromSeconds(0.5))
};
fadeOutAnimation.Completed += fadeOutAnimation_Completed;
BeginAnimation(OpacityProperty, fadeOutAnimation);
}
#endregion
#region 닫기 타이머 시작하기 - StartCloseTimer()
/// <summary>
/// 닫기 타이머 시작하기
/// </summary>
private void StartCloseTimer()
{
DispatcherTimer timer = new DispatcherTimer();
timer.Tick += (s, e) => Close();
timer.Interval = DisplayTime;
timer.Start();
}
#endregion
}