Windows Phone 8.1中数据绑定之一

来源:互联网 发布:流程的优化分类 编辑:程序博客网 时间:2024/06/12 11:30

数据绑定,顾名思义,两个要义:一是数据,而是绑定

然而再一想,是谁将数据绑定到谁的属性上面,这一句话就道出了数据绑定的四个关键对象:绑定目标对象、目标属

性、绑定源、绑定源中要绑定的值。


根据绑定对象和绑定源来划分,其实无非就两种:

UI控件作为绑定对象,UI控件作为绑定源(数据源)

UI控件作为绑定对象,自定义的数据对象作为绑定源(数据源,.cs中定义)

不管是前者还是后者,无非采用的是Binding这个神器来设置的。主要用到Binding的ElementName属性和Path属性

两个属性。ElementName属性赋值为数据源控件的Name的值,Path属性则赋值为数据源控件的某个属性,这个属

性就是数据源控件的一个数据变化的反映。

此种方法较为灵活,有多种情形和表现形式,如下(只写了Path,不代表没有ElementName,对于UI到UI是有

ElementName,对于自定义数据源到UI是没有ElementName的):

(1)Binding Path=PropertyName

作为UI绑定UI:Binding ElementName=Grid,Path=Width

作为自定义数据源绑定:Binding Path = propertyName

(2)Binding Path = Screen.Height,可以看作绑定源是一个电脑对象,Screen(屏幕)是电脑里的一个属性,而

Height(高度)又是Screen的一个属性,这样就可以看出绑定了数据源的属性的属性

(3)Binding Path = (Grid.Column)

(4)Binding Path = Screen[1]

(5)Binding Path = Screen.ScreenInfo[productor,date]

(6)Binding Path = "[(sys:Int32)3,(sys:Int32)22]",sys表示System命名空间

(7)Binding Path = /,如果源为集合视图,则制定当前项

(8)Binding Path = /Screen/publisher

(9)Binding Path = .相当于Binding

再者,绑定有三种绑定模式:OneTime,OneWay,TwoWay

OneTime:表示一次绑定,即初次绑定一次,之后不管,数据源改变也更新不到绑定目标上。也就是说只显示数据而

不进行数据的更新的静态数据绑定。

OneWay:默认绑定模式。表示单向绑定,即当绑定源数据发生更改时,会将变化更新到绑定目标上,适用于显示变

化的数据。

TwoWay:表示双向绑定,即绑定源数据和绑定目标上的数据发生改变时,会相互影响,相互反映。这种做法会损耗

额外的性能,不到必须使用最好不要用。


其实对于UI控件作为绑定对象,UI控件作为绑定源(数据源)这种情况无非是使用上文的Binding设置做文章,重点还是

得在UI控件作为绑定对象,自定义的数据对象作为绑定源(数据源,.cs中定义)的这种情形上,因为这种相对使用的更

广,更加自由灵活,对于业务和逻辑需要更加贴近。


一般是利用DataContext属性绑定到源对象,DataContext属性表示Windows Phone的UI元素的数据上下文,可以

给UI元素提供数据。如果给页面最顶层的Page设置其DatContext属性绑定到源对象,那么整个页面都可以使用该数

据源提供的数据。具体示例代码如下:

XAML代码:

<Page    x:Class="App1.DataBindDemo1"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:local="using:App1"    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    mc:Ignorable="d"    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">    <Grid>        <TextBlock HorizontalAlignment="Center" Text="{Binding test}" FontSize="20"/>    </Grid></Page>

.CS代码:

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Runtime.InteropServices.WindowsRuntime;using Windows.Foundation;using Windows.Foundation.Collections;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;using Windows.UI.Xaml.Controls.Primitives;using Windows.UI.Xaml.Data;using Windows.UI.Xaml.Input;using Windows.UI.Xaml.Media;using Windows.UI.Xaml.Navigation;// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkID=390556 上有介绍namespace App1{    /// <summary>    /// 可用于自身或导航至 Frame 内部的空白页。    /// </summary>    public sealed partial class DataBindDemo1 : Page    {        public class TestData        {            public string test { get; set; }        }        TestData testData = new TestData() { test="这是绑定的数据!" };        public DataBindDemo1()        {            this.InitializeComponent();            this.DataContext = testData;        }        /// <summary>        /// 在此页将要在 Frame 中显示时进行调用。        /// </summary>        /// <param name="e">描述如何访问此页的事件数据。        /// 此参数通常用于配置页。</param>        protected override void OnNavigatedTo(NavigationEventArgs e)        {        }    }}


0 0
原创粉丝点击