Silverlight入门学习(32)

Silverlight入门学习(32),第1张

概述原文地址: http://www.dingos.cn/index.php?topic=2000.0 第三十二章   介绍在 Silverlight 绑定数据 数据绑定可以动态的将对象属性绑定到 Silverlight 中 UI 控件的属性中。 例 如,一个简单的 Silverlight 控件用于接收用户的名字和地址。可以创建一个 xaml 控件具有以下文本字段: 1. Name 2. Addres

原文地址: 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 控件。当用户改变值时,数据源对象将自动更改。我们所需要做的是,保存修改的数据源对象到数据 库。

总结

以上是内存溢出为你收集整理的Silverlight入门学习(32)全部内容,希望文章能够帮你解决Silverlight入门学习(32)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1033992.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存