Skip to content

Instantly share code, notes, and snippets.

@kuttsun
Last active December 11, 2016 15:36
Show Gist options
  • Save kuttsun/97238332d97119ba1fd949989fe5aa06 to your computer and use it in GitHub Desktop.
Save kuttsun/97238332d97119ba1fd949989fe5aa06 to your computer and use it in GitHub Desktop.
/// <summary>
/// コマンドの実装
/// </summary>
class ButtonCommand : ICommand
{
private MainWindowViewModel MainWindow;
/// <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 (MainWindow?.SampleText == string.Empty ? false : true);
}
/// <summary>
/// コマンドが起動される際に呼び出すメソッドを定義
/// </summary>
/// <param name="parameter">コマンドで使用されたデータ。 コマンドにデータを渡す必要がない場合は、このオブジェクトを null に設定できる。</param>
public void Execute(object parameter)
{
MessageBox.Show(MainWindow.SampleText);
}
/// <summary>
/// コンストラクタ
/// </summary>
/// <param name="viewmodel">ViewModelのインスタンスへの参照</param>
public ButtonCommand(MainWindowViewModel mainWindow)
{
MainWindow = mainWindow;
}
}
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" TextWrapping="Wrap" Text="{Binding SampleText, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
<Label x:Name="label" Content="{Binding SampleLabel}" HorizontalAlignment="Left" Margin="10,38,0,0" VerticalAlignment="Top"/>
<Button x:Name="button" Command="{Binding Button}" Content="Button" HorizontalAlignment="Left" Margin="135,13,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</Window>
/// <summary>
/// MainWindowに対するViewModel
/// </summary>
class MainWindowViewModel : ViewModelBase
{
// バインディング対象のプロパティ
public ButtonCommand 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 ButtonCommand(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment