あなたの説明をする簡単なアプリを作成しただけで、それは私の仕事です。
XAML:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<TextBox Grid.Row="0" AcceptsReturn="True" Height="50"
Text="{Binding Path=Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<Button Grid.Row="1" Click="Button_Click">Button</Button>
</Grid>
</Window>
ビューモデル:
class ViewModel : INotifyPropertyChanged
{
private string text = string.Empty;
public string Text
{
get { return this.text; }
set
{
this.text = value;
this.OnPropertyChanged("Text");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propName)
{
var eh = this.PropertyChanged;
if(null != eh)
{
eh(this, new PropertyChangedEventArgs(propName));
}
}
}
ViewModel
のインスタンスがWindow
ためDataContext
として設定されています。最後に、Button_Click()
の実装は次のとおりです。
private void Button_Click(object sender, RoutedEventArgs e)
{
this.model.Text = "Hello\r\nWorld";
}
(私はビューが本当に直接のViewModelのText
プロパティを変更するべきではありませんが、これは単に迅速なサンプルアプリケーションであることを認識しています。)
これは、その結果TextBox
の最初の行に "Hello"という単語があり、2番目の行に "World"があります。
コードを投稿すると、このサンプルとは何が違うのか分かりますか?
ありがとうAndy、私は私の最後に問題を考え出した。ご協力いただきありがとうございます。 – deepak