2012-04-25 9 views
2

への結合、私は私のページクラスのプロパティ(MainStudent)を持っている:プロパティ

学生クラスは、いくつかのメソッドを持っている(MainStudent財産の姓のプロパティをバインドするための最良の方法は何たとえば
public partial class AddStudent : Page 
{ 
    public AddStudent() 
    { 
     InitializeComponent(); 
     MainStudent = new Student(); 
    } 

    public Student MainStudent 
    { 
     get; 
     set; 
    } 
} 

:姓、 LastName。)to XAMLのTextBoxのTextプロパティ?

答えて

2

Studentのみを参照すると、現在のコンストラクタの最後の行として、DataContextを直接設定できます。

XAMLで210
this.DataContext = this.MainStudent; 

、その後、直接学生のプロパティにバインディングを実行します。

<TextBox Text="{Binding FistName}"></TextBox> 

さもないと、あなたが自分自身にあなたのPageののDataContextを設定することができます。

this.DataContext = this; 

をとのようなバインディングを実行します。

<TextBox Text="{Binding MainStudent.FirstName}"></TextBox> 
0

「MainStudent」を含むクラスを使用すると、さらに助けが必要な場合は、単に頼む例

<Grid DataContext="{StaticRessource YourViewModelDefinedInRessources}"> 
    <TextBox Text="{Binding MainStudent.FirstName}" /> 
    <TextBox Text="{Binding MainStudent.LastName}" /> 
</Grid> 

についてあなたのDataContext されている場合は、次を使用します。あなたのPageは、その性質を持っている場合)

1
<TextBox Text="{Binding MainStudent.Firstname, UpdateSourceTrigger=PropertyChanged}"/> 
+0

UpdateSourceTrigger = PropertyChangedでは、各文字を入力するとセッターが呼び出されることに注意してください。それ以外の場合、Setterはフォーカスを失うと呼び出されます。 – sebastianmehler

+1

モデルが変更されたことをビューに通知し、ビューを更新できます。 http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.propertychanged.aspx –