ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [WPF]MVVM Sample
    C#/WPF_예제소스 2022. 7. 11. 11:30

    MVVM_Basics.zip
    0.47MB


    ViewModelBase

    public class ViewModelBase : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler? PropertyChanged;
    
            protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    CommandBase

    public abstract class CommandBase : ICommand
        {
            public event EventHandler? CanExecuteChanged;
    
            public virtual bool CanExecute(object? parameter)
            {
                return true;
            }
            public abstract void Execute(object? parameter);
            protected void OnCanExecuteChanged()
            {
                CanExecuteChanged?.Invoke(this, new EventArgs());
            }
        }

    RelayCommand

    public class RelayCommand : CommandBase
        {
            private readonly Predicate<object> _canExecute;
            private readonly Action<object> _execute;
    
            public RelayCommand(Action<object> execute) : this(execute, null) { }
            public RelayCommand(Action<object> execute, Predicate<object> canExecute)
            {
                _canExecute = canExecute;
                _execute = execute ?? throw new ArgumentNullException("execute");
            }
    
            public override bool CanExecute(object? parameter)
            {
                return _canExecute == null || _canExecute(parameter);
            }
            public override void Execute(object? parameter)
            {
                _execute(parameter);
            }
        }

     

     

     

    728x90

    'C# > WPF_예제소스' 카테고리의 다른 글

    [WPF] App.config  (0) 2022.09.05
    [WPF] foreach Control  (0) 2022.08.17
    [WPF] OnPropertyChanged  (0) 2022.07.01
    [WPF]모눈종이 그리기  (0) 2022.06.29
    [C#]Canvas to BitmapImage  (0) 2022.05.24

    댓글

Designed by Tistory.