CTOCIO IT专家网

天极传媒 比特网 | 天极网 | IT专家网 | IT商网 | 52PK游戏网 | 手机天极 | IT分众 |
IT专家网搜索

您现在的位置: 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;
  }
  }
  注意,如果我们采取捷径,使 Name 和 Age 成为公共字段以便简化代码,则 Avalon 不会绑定到它们。Avalon 只会绑定到公共属性。绑定到 Person 对象的一个实例时,将如下所示:    

<!-- 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;
  }
}

  

?9?? 9 1 2 3 4 5 6 7 8 :

网友评论

笔名 
请您注意:遵守国家有关法律、法规,尊重网上道德,承担一切因您的行为而直接或间接引起的法律责任。    IT专家网友拥有管理笔名和留言的一切权利。
  • 周排行榜
  • 月排行榜

邮件订阅

该文章的读者还阅读了

天极服务 | 关于我们 | 网站律师 | 加入我们 | 联系我们 | 广告业务 | 友情链接 | 我要挑错
All Rights Reserved, Copyright 2004-2008, Ctocio.com.cn
渝ICP证B2-20030003号 如有意见请与我们联系 powered by 天极内容管理平台CMS4i