2020/08/21 - [WPF] - 02. WPF MVVM(1) View-ViewModel 연결
2020/08/21 - [WPF] - 03. WPF MVVM(2) INotifyPropertyChanged
View에서 일어나는 이벤트 가져오기
메뉴가 선택하면 다음과 같은 이벤트가 실행된다 해보자.
<Menu Background="Transparent" FontSize="16pt">
<MenuItem Header="기본 정보" Command="{Binding BasicsButtonCommand}" />
</Menu>
ViewModel에 다음과 같이 설정한다. BasicsButtonCommand 이름에 주목하자.
public AllotmentView ViewWindow { get; set; }
public DataTable Grade1Grid { get { return grade1Grid; } set { grade1Grid = value; OnPropertyChanged(); } }
public DataRowView GradeRowView { set { GradeGrideClick(value); OnPropertyChanged(); } }
public ICommand BasicsButtonCommand { get; set; }
public AllotmentViewModel(AllotmentView viewWindow)
{
this.ViewWindow = viewWindow;
BasicsButtonCommand = new RelayCommand<Button>(BasicsClick, CanExcuteMethod);
}
private void BasicsClick(Button sender)
{
//이벤트
}
private bool CanExcuteMethod(object arg)
{
//활성화
return true;
}
public class RelayCommand<T> : ICommand
{
private Action<T> execute;
private Predicate<object> canExecute;
private event EventHandler CanExecuteChangedInternal;
public RelayCommand(Action<T> execute)
: this(execute, DefaultCanExecute)
{
}
public RelayCommand(Action<T> execute, Predicate<object> canExecute)
{
this.execute = execute ?? throw new ArgumentNullException("execute");
this.canExecute = canExecute ?? throw new ArgumentNullException("canExecute");
}
public event EventHandler CanExecuteChanged
{
add
{
CommandManager.RequerySuggested += value;
this.CanExecuteChangedInternal += value;
}
remove
{
CommandManager.RequerySuggested -= value;
this.CanExecuteChangedInternal -= value;
}
}
public bool CanExecute(object parameter)
{
return this.canExecute != null && this.canExecute(parameter);
}
public void Execute(object parameter)
{
this.execute((T)parameter);
}
public void OnCanExecuteChanged()
{
CanExecuteChangedInternal?.Invoke(this, EventArgs.Empty);
}
public void Destroy()
{
this.canExecute = _ => false;
this.execute = _ => { return; };
}
private static bool DefaultCanExecute(object parameter)
{
return true;
}
}
public class RelayCommand : ICommand
{
private Action execute;
private Func<bool> canExecute;
private event EventHandler CanExecuteChangedInternal;
public RelayCommand(Action execute)
: this(execute, DefaultCanExecute)
{
}
public RelayCommand(Action execute, Func<bool> defaultCanExecute)
{
this.execute = execute;
this.canExecute = defaultCanExecute;
}
public event EventHandler CanExecuteChanged
{
add
{
CommandManager.RequerySuggested += value;
this.CanExecuteChangedInternal += value;
}
remove
{
CommandManager.RequerySuggested -= value;
this.CanExecuteChangedInternal -= value;
}
}
public bool CanExecute(object parameter)
{
return this.canExecute != null && this.canExecute();
}
public void Execute(object parameter)
{
this.execute();
}
public void OnCanExecuteChanged()
{
CanExecuteChangedInternal?.Invoke(this, EventArgs.Empty);
}
private static bool DefaultCanExecute()
{
return true;
}
}
'WPF' 카테고리의 다른 글
07. WPF DataGrid Click 위치로 정보 가져오기 (0) | 2020.08.21 |
---|---|
06. WPF DataGridViewHelper (0) | 2020.08.21 |
03. WPF MVVM(2) INotifyPropertyChanged (0) | 2020.08.21 |
02. WPF MVVM(1) View-ViewModel 연결 (0) | 2020.08.21 |
01. WPF DataGrid Text Center (0) | 2020.05.30 |