Last active
December 11, 2016 15:34
-
-
Save kuttsun/3346b675e0348bf64d1bcfe28ee8a9eb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// コマンドの実装 | |
/// </summary> | |
class DelegateCommand : ICommand | |
{ | |
/// <summary> | |
/// Execute の実体を保持します。 | |
/// </summary> | |
private Action<object> execute; | |
/// <summary> | |
/// CanExecute の実態を保持します。 | |
/// </summary> | |
private Func<object, bool> canExecute; | |
/// <summary> | |
/// コンストラクタ | |
/// </summary> | |
/// <param name="execute">Execute の実体を指定</param> | |
/// <param name="canExecute">CanExecute の実体を指定</param> | |
public DelegateCommand(Action<object> execute, Func<object, bool> canExecute) | |
{ | |
this.execute = execute; | |
this.canExecute = canExecute; | |
} | |
/// <summary> | |
/// コマンドを実行するかどうかに影響するような変更があった場合に発生する | |
/// </summary> | |
public event EventHandler CanExecuteChanged; | |
/// <summary> | |
/// CanExecuteChangedイベントを発行する | |
/// </summary> | |
public void OnCanExecuteChanged() | |
{ | |
CanExecuteChanged?.Invoke(this, EventArgs.Empty); | |
} | |
/// <summary> | |
/// 現在の状態でコマンドが実行可能かどうかを決定するメソッドを定義 | |
/// </summary> | |
/// <param name="parameter">コマンドで使用されたデータ。 コマンドにデータを渡す必要がない場合は、このオブジェクトを null に設定できる。</param> | |
/// <returns>コマンドを実行できる場合は true。それ以外の場合は false。</returns> | |
public bool CanExecute(object parameter) | |
{ | |
return (canExecute == null ? true : canExecute(parameter)); | |
} | |
/// <summary> | |
/// コマンドが起動される際に呼び出すメソッドを定義 | |
/// </summary> | |
/// <param name="parameter">コマンドで使用されたデータ。 コマンドにデータを渡す必要がない場合は、このオブジェクトを null に設定できる。</param> | |
public void Execute(object parameter) | |
{ | |
execute?.Invoke(parameter); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// MainWindowに対するViewModel | |
/// </summary> | |
class MainWindowViewModel : ViewModelBase | |
{ | |
// バインディング対象のプロパティ | |
public DelegateCommand Button { get; set; } | |
// バインディングされる値を保持するフィールド | |
private string sampleText; | |
// バインディング対象のプロパティ | |
public string SampleText | |
{ | |
get | |
{ | |
return sampleText; | |
} | |
set | |
{ | |
sampleText = value; | |
// 変更をViewに通知する | |
OnPropertyChanged(nameof(SampleText)); | |
// ボタンの無効表示に影響するので、CanExecuteChanged イベントを発行する | |
Button?.OnCanExecuteChanged(); | |
// ラベルの値も連動させる | |
SampleLabel = value; | |
} | |
} | |
// バインディングされる値を保持するフィールド | |
private string sampleLabel = ""; | |
// バインディング対象のプロパティ | |
public string SampleLabel | |
{ | |
get | |
{ | |
return sampleLabel; | |
} | |
set | |
{ | |
sampleLabel = value; | |
// 変更をViewに通知する | |
OnPropertyChanged(nameof(SampleLabel)); | |
} | |
} | |
/// <summary> | |
/// コンストラクタ | |
/// </summary> | |
public MainWindowViewModel() | |
{ | |
SampleText = "Sample"; | |
SampleLabel = "Sample"; | |
Button = new DelegateCommand( | |
x => MessageBox.Show(SampleText), | |
x => (SampleText == string.Empty ? false : true) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment