Last active
December 11, 2016 15:36
-
-
Save kuttsun/97238332d97119ba1fd949989fe5aa06 to your computer and use it in GitHub Desktop.
This file contains 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
<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> |
This file contains 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 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