2011-12-19 80 views
2

欲一個實體屬性(比如Salary)綁定到XAML元件(如TextBox.Text) 的特性,並使用該結合的TextBoxText保存到這勢必作爲工資字段實體屬性爲「文本」的一些TextBox綁定XAML元素的實體雙向

類似以下內容:

<Grid DataContext="Employee"> 
    <TextBox Text="{Binding Path=Salary, Mode=TwoWay}"/> 
</Grid> 

回答

3

你可以在xaml中綁定屬性 - 所以你的薪水必須是屬性而不是字段。如果您的員工是具有薪水的班級,您可以將datacontext設置爲其實例。你可以用xaml或代碼隱藏或綁定來完成它。

public class Employee //implement INotifyPropertyChanged to get the power of binding :) 
{ 
    public decimal Salary {get;set} 
} 

view.xaml

<Grid> 
<Grid.DataContext> 
     <local:Employee/> 
    </Grid.DataContext> 
    <TextBox Text="{Binding Path=Salary, Mode=TwoWay}"/> 
</Grid> 

可以設置的datacontext在許多方面

0

不,你不能做這樣的。您不能將類名稱設置爲DataContext。它應該是Employee類的實例。