프로그래밍/C#

[C#] WPF Window Close 이벤트 처리

Victory_HA 2022. 10. 27. 10:00

Case1

출처: https://ehclub.co.kr/1987 [언제나 휴일:티스토리]

  using System;
  using System.Windows;

  namespace Ex_윈도우_이벤트_핸들러_추가하기
  {
      public partial class Window1 : Window
      {
          public Window1()
          {             
                  InitializeComponent();
                  this.Closed += new EventHandler(Window1_Closed);
          }
          void  Window1_Closed(object sender, EventArgs e)
          {            
                  MessageBox.Show("창이 닫혔음");
          }
      }
  }

Case2

ClassButtonFilterDialogViewModel.cs

public class ClassButtonFilterDialogViewModel : ViewModelBase
{
     #region Meber Valiables
     public event EventHandler<string> ClosedWindow;

     public ICommand OkCommand { get; private set; }

     public ClassButtonFilterDialogViewModel()
     {
         OkCommand = new DelegateCommand(_okCommandAction);
     }

     private void _okCommandAction()
     {
         ClosedWindow(this, null);
     }

}

ClassButtonFilterDialog.xaml

public ClassButtonFilterDialog(ClassButtonFilterDialogViewModel classButtonFilterViewModel)
{
    InitializeComponent();
    DataContext = classButtonFilterViewModel;
    classButtonFilterViewModel.ClosedWindow += _closedWindow;
    //classButtonFilterViewModel.ClosedWindow += new EventHandler<string>(_closedWindow);

}

private void _closedWindow(object sender, string msg)
{
    this.Close();
}

'프로그래밍 > C#' 카테고리의 다른 글

[C#] n이하의 홀수 덧셈  (0) 2022.12.04
[C#] Enum의 Index값 가져오기  (0) 2022.11.08
[C#] List 타입 변경  (0) 2022.10.13
[C#] 리스트에서 랜덤으로 값 가져오기  (0) 2022.10.11
[WPF] INotifyPropertyChanged 구현  (0) 2022.10.07