Skip to content

Instantly share code, notes, and snippets.

@kuttsun
Last active September 15, 2016 12:38
Show Gist options
  • Save kuttsun/b4db11f45770c68e2cfe0c766aacee50 to your computer and use it in GitHub Desktop.
Save kuttsun/b4db11f45770c68e2cfe0c766aacee50 to your computer and use it in GitHub Desktop.
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// バインディング対象のインスタンスを、MainWindowViewModelに設定
this.DataContext = new MainWindowViewModel();
}
}
<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"/>
</Grid>
</Window>
/// <summary>
/// MainWindowに対するViewModel
/// </summary>
class MainWindowViewModel
{
// バインディングされる値を保持するフィールド
private string sampleText_;
// バインディング対象のプロパティ
public string SampleText
{
get
{
return sampleText_;
}
set
{
sampleText_ = value;
SampleLabel = value;// ラベルの値も連動させる
}
}
// バインディングされる値を保持するフィールド
private string sampleLabel_ = "";
// バインディング対象のプロパティ
public string SampleLabel
{
get
{
return sampleLabel_;
}
set
{
sampleLabel_ = value;
}
}
/// <summary>
/// コンストラクタ
/// </summary>
public MainWindowViewModel()
{
SampleText = "Sample";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment