原文地址: http://www.dingos.cn/index.php?topic=2000.0
第三十二章 介绍在 Silverlight 绑定数据
数据绑定可以动态的将对象属性绑定到 Silverlight 中 UI 控件的属性中。
例 如,一个简单的 Silverlight 控件用于接收用户的名字和地址。可以创建一个 xaml 控件具有以下文本字段:
1. Name
2. Address1
3. Address2
4. City
5. State
6. Zip code
在这里 XAML 中都有一个 Grid 用于摆放示例中的控件:
< Grid x : Name ="LayoutRoot" Background ="AliceBlue" Width ="300" Height ="300">
< Grid.RowDeFinitions >
< RowDeFinition Height ="30"></ RowDeFinition >
< RowDeFinition Height ="30"></ RowDeFinition >
< RowDeFinition Height ="30"></ RowDeFinition >
< RowDeFinition Height ="30"></ RowDeFinition >
< RowDeFinition Height ="30"></ RowDeFinition >
< RowDeFinition Height ="30"></ RowDeFinition >
< RowDeFinition Height ="30"></ RowDeFinition >
< RowDeFinition Height ="30"></ RowDeFinition >
< RowDeFinition Height ="30"></ RowDeFinition >
</ Grid.RowDeFinitions >
< Grid.ColumnDeFinitions >
< ColumnDeFinition Width ="100"></ ColumnDeFinition >
< ColumnDeFinition Width ="200"></ ColumnDeFinition >
</ Grid.ColumnDeFinitions >
< TextBlock Text ="Name" Grid.Row ="0" Grid.Column ="0"></ TextBlock >
< TextBlock Text ="Address 1" Grid.Row ="1" Grid.Column ="0"></ TextBlock >
< TextBlock Text ="Address 2" Grid.Row ="2" Grid.Column ="0"></ TextBlock >
< TextBlock Text ="City" Grid.Row ="3" Grid.Column ="0"></ TextBlock >
< TextBlock Text ="State" Grid.Row ="4" Grid.Column ="0"></ TextBlock >
< TextBlock Text ="Zipcode" Grid.Row ="5" Grid.Column ="0"></ TextBlock >
< TextBox x : Name ="txtName" Text ="{ Binding Name , Mode =TwoWay}" Grid.Row ="0"
Grid.Column ="1" Height ="20" Width ="100"></ TextBox >
< TextBox x : Name ="txtAddress1" Text ="{ Binding Address1 , Mode =TwoWay}"
Grid.Row ="1" Grid.Column ="1" Height ="20" Width ="100"></ TextBox >
< TextBox x : Name ="txtAddress2" Text ="{ Binding Address2 , Mode =TwoWay}"
Grid.Row ="2" Grid.Column ="1" Height ="20" Width ="100"></ TextBox >
< TextBox x : Name ="txtCity" Text ="{ Binding City , Mode =TwoWay}" Grid.Row ="3"
Grid.Column ="1" Height ="20" Width ="100"></ TextBox >
< TextBox x : Name ="txtState" Text ="{ Binding State , Mode =TwoWay}" Grid.Row ="4"
Grid.Column ="1" Height ="20" Width ="100"></ TextBox >
< TextBox x : Name ="txtZipcode" Text ="{ Binding Zipcode , Mode =TwoWay}"
Grid.Row ="5" Grid.Column ="1" Height ="20" Width ="100"></ TextBox >
< Button Grid.Row ="6" Grid.Column ="0" Grid.ColumnSpan ="2" Width ="50"
Content ="Save" x : Name ="btnSave" Click ="btnSave_Click"></ Button >
</ Grid >
在上面的 XAML 定义了一个 7 行 2 列的网格。这些控件通过使用各自的 Grid.Row 和 Grid.Column 属性防止合适的行、列中。当 Silverlight 控件放在 Web 页面显示如下所示:
现在创建一个名 为“ Address ”的类,它的各个属性表示我们需要的域。看“ Address ”类的示例代码:
public class Address {
public string Name { get ; set ; }
public string Address1 { get ; set ; }
public string Address2 { get ; set ; }
public string City { get ; set ; }
public string State { get ; set ; }
public string Zipcode { get ; set ; }
}
转到 XAML 类的后台代码文件,按如下方式 创建一个 Address 类的实例:
Address address;
在 XAML 页面类的构造方法中,初始化 Address 类,按如下方式绑定到 UI 元素:
address = new Address ();
txtName.DataContext = address;
txtAddress1.DataContext = address;
txtAddress2.DataContext = address;
txtCity.DataContext = address;
txtState.DataContext = address;
txtZipcode.DataContext = address;
在上面代码中,将“ Address ”对象设置到每个 UI 控件的 DataContext 属性。但是,如何控制使用的每个 address 对象的属性呢?这在 XAML 中处理。看一看“ txtAddress1 ”控件。可以看到“ Text ”属性按如下设置:
Text ="{ Binding Address1 , Mode =TwoWay}
上面代码行大 运了使用数据绑定为控件的“ Text ”属性赋值,它无论哪个对象都绑定在“ Address1 ”属性。此外,状态模式为“ TwoWay ”,表示这个值可以从对象读取,也可以设置“ Text ”属性当文本框控件中的值发生改 变时,会保存回数据源。在这个例子中,绑定 Address 对象到文本框控件。当加载时,将显示对象的默认值到文本框。当用户改变值后,数据源对象将从文本 框中更新值。
通常,我们添加一个新地址,这个对象将初始化为空值且文 本框为清空。当我们编译一个已存在的地址,这个对象将从数据库中填充值并使用数据绑定显示在 UI 控件。当用户改变值时,数据源对象将自动更改。我们所需要做的是,保存修改的数据源对象到数据 库。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。