您现在的位置: IT专家网 > WinSystem子站 > 技巧
让人迷恋 Avalon 数据绑定
作者: Chris Sells, 出处:微软, 责任编辑: 韩博颖,
2008-05-26 08:38
讨论 Avalon,并且将数据绑定引入其基于 Longhorn 的 Solitaire 应用程序。
更好的绑定
绑定到单个对象上的单个属性是很有趣的,但是让我们尝试某种稍微复杂一点儿的做法。例如,设想有一个类,它具有两个公共的读写属性:
| public class Person { string name; public string Name { get { return this.name; } set { this.name = value; } } int age; public int Age { get { return this.Age; } set { this.Age = value; } } public Person(string name, int age) { this.name = name; this.age = age; } } |
| <!-- Window1.xaml --> <Window ... > <GridPanel Columns="2"> <Text>Name</Text> <TextBox Text="*Bind(Path=Name)"/> <Text>Age</Text> <TextBox Text="*Bind(Path=Age)"/> <Border /> <Button ID="showButton">Show</Button> <Border /> <Button ID="birthdayButton">Birthday</Button> </GridPanel> </Window> // Window1.xaml.cs ... partial class Window1 : Window { Person person = new Person("John", 10); void Window1_Loaded(object sender, EventArgs e) { this.DataContext = this.person; showButton.Click += showButton_Click; birthdayButton.Click += birthdayButton_Click; } void showButton_Click(object sender, ClickEventArgs e) { MessageBox.Show( string.Format( "Person= {0}, age {1}", this.person.Name, this.person.Age)); } void birthdayButton_Click(object sender, ClickEventArgs e) { ++this.person.Age; } } |
- 本文关键词:

