첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

▶ RelayCommand.cs

using System;
using System.Windows.Input;

/// <summary>
/// 릴레이 명령
/// </summary>
public class RelayCommand : ICommand
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Event
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 실행 가능 여부 변경시 - CanExecuteChanged

    /// <summary>
    /// 실행 가능 여부 변경시
    /// </summary>
    public event EventHandler CanExecuteChanged
    {
        add
        {
            CommandManager.RequerySuggested += value;
        }
        remove
        {
            CommandManager.RequerySuggested -= value;
        }
    }

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Field
    ////////////////////////////////////////////////////////////////////////////////////////// Private

    #region Field

    /// <summary>
    /// 실행 액션
    /// </summary>
    private readonly Action<object> executeAction;

    /// <summary>
    /// 실행 가능 여부 프레디킷
    /// </summary>
    private readonly Predicate<object> canExecutePredicate;

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 생성자 - RelayCommand(executeAction, canExecutePredicate)

    /// <summary>
    /// 생성자
    /// </summary>
    /// <param name="executeAction">실행 액션</param>
    /// <param name="canExecutePredicate">실행 가능 여부 프레디킷</param>
    public RelayCommand(Action<object> executeAction, Predicate<object> canExecutePredicate)
    {
        if(executeAction == null)
        {
            throw new ArgumentNullException("executeAction");
        }

        this.executeAction       = executeAction;
        this.canExecutePredicate = canExecutePredicate;
    }

    #endregion
    #region 생성자 - RelayCommand(executeAction)

    /// <summary>
    /// 생성자
    /// </summary>
    /// <param name="executeAction">실행 액션</param>
    public RelayCommand(Action<object> executeAction) : this(executeAction, null)
    {
    }

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Method
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 실행하기 - Execute(parameter)

    /// <summary>
    /// 실행하기
    /// </summary>
    /// <param name="parameter">파라미터</param>
    public void Execute(object parameter)
    {
        this.executeAction(parameter);
    }

    #endregion
    #region 실행 가능 여부 구하기 - CanExecute(parameter)

    /// <summary>
    /// 실행 가능 여부 구하기
    /// </summary>
    /// <param name="parameter">파라미터</param>
    /// <returns>실행 가능 여부</returns>
    public bool CanExecute(object parameter)
    {
        return this.canExecutePredicate == null ? true : this.canExecutePredicate(parameter);
    }

    #endregion
}
728x90
반응형
그리드형
Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요