■ UserControl 클래스를 사용해 알림 메시지 컨트롤을 만드는 방법을 보여준다.
▶ NotificationControl.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<UserControl x:Class="TestProject.NotificationControl" Name="userControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" BorderThickness="0" BorderBrush="Transparent" Background="Transparent"> <Border Name="border" CornerRadius="10" BorderThickness="{Binding ElementName=userControl, Path=BorderThickness}" BorderBrush="{Binding ElementName=userControl, Path=BorderBrush}" Padding="10" Background="{Binding ElementName=userControl, Path=Background}" Opacity="0"> <Border.Triggers> <EventTrigger RoutedEvent="Border.Loaded"> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="00:00:00.5" From="0" To="1" /> </Storyboard> </BeginStoryboard> </EventTrigger> </Border.Triggers> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <TextBlock Name="textBlock" Grid.Column="0" VerticalAlignment="Center" TextWrapping="Wrap" Background="Transparent" Foreground="{Binding ElementName=userControl, Path=Foreground}" /> <Button Name="closeButton" Grid.Column="1" VerticalAlignment="Top" Margin="10 0 0 0" Padding="5" Content="X" /> </Grid> </Border> </UserControl> |
▶ NotificationControl.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
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 } |
▶ NotificationHelper.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
using System; using System.Collections.Generic; using System.Windows.Controls; namespace TestProject; /// <summary> /// 알림 헬퍼 /// </summary> public class NotificationHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 알림 컨트롤 큐 /// </summary> private readonly Queue<NotificationControl> notificationControlQueue = new Queue<NotificationControl>(); /// <summary> /// 컨테이너 패널 /// </summary> private readonly Panel containerPanel; /// <summary> /// 현재 알림 컨트롤 /// </summary> private NotificationControl currentNotificationControl; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - NotificationHelper(containerPanel) /// <summary> /// 생성자 /// </summary> /// <param name="containerPanel">컨테이너 패널</param> public NotificationHelper(Panel containerPanel) { this.containerPanel = containerPanel; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 알림 표시하기 - ShowNotification(notificationControl) /// <summary> /// 알림 표시하기 /// </summary> /// <param name="notificationControl"></param> public void ShowNotification(NotificationControl notificationControl) { notificationControl.Closed += notificationControl_Closed; if(this.currentNotificationControl == null) { ShowNextNotification(notificationControl); } else { this.notificationControlQueue.Enqueue(notificationControl); } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 알림 컨트롤 닫은 경우 처리하기 - notificationControl_Closed(sender, e) /// <summary> /// 알림 컨트롤 닫은 경우 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void notificationControl_Closed(object sender, EventArgs e) { this.currentNotificationControl = null; if(this.notificationControlQueue.Count > 0) { ShowNextNotification(this.notificationControlQueue.Dequeue()); } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 다음 알림 표시하기 - ShowNextNotification(notificationControl) /// <summary> /// 다음 알림 표시하기 /// </summary> /// <param name="notificationControl">알림 컨트롤</param> private void ShowNextNotification(NotificationControl notificationControl) { this.currentNotificationControl = notificationControl; this.containerPanel.Children.Add(notificationControl); } #endregion } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <Grid Name="grid"> <Button Name="showButton" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="10" Content="알림 메시지 표시" /> </Grid> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
using System; using System.Windows; using System.Windows.Media; namespace TestProject; /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 알림 헬퍼 /// </summary> private NotificationHelper helper; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); helper = new NotificationHelper(this.grid); this.showButton.Click += showButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 알림 메시지 표시 버튼 클릭시 처리하기 - showButton_Click(sender, e) /// <summary> /// 알림 메시지 표시 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void showButton_Click(object sender, RoutedEventArgs e) { NotificationControl notificationControl = new NotificationControl { HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Bottom, Margin = new Thickness(20), BorderThickness = new Thickness(3), BorderBrush = Brushes.DarkGray, Background = new SolidColorBrush(Color.FromRgb(45, 45, 45)), Foreground = Brushes.White, Message = $"{DateTime.Now}", DisplayTime = TimeSpan.FromMilliseconds(3000) }; this.helper.ShowNotification(notificationControl); } #endregion } |