silverlight 数据绑定简单示例(markup extension)
2010-06-19 09:20
507 查看
这个例子中被绑定的数据是一个owner类的实例,现在silverlight项目中添加一个owner类
Class Owner
public class Owner
{
public int OwnerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string ZipCode { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
public DateTime BirthDate { get; set; }
public DateTime CustomerSince { get; set; }
public string ImageName { get; set; }
public DateTime LastActivityDate { get; set; }
public double CurrentBalance { get; set; }
public double LastActivityAmount { get; set; }
}
然后在MainPage.xaml.cs中创建一个Owner类的实例
MainPage.xaml.cs
public MainPage()
{
InitializeComponent();
InitializeOwner();
}
private void InitializeOwner()
{
owner = new Owner();
owner.OwnerId = 1234567;
owner.FirstName = "Zhu";
owner.LastName = "jz";
owner.Address = "ChaoYang BJ";
owner.ZipCode = "100001";
owner.City = "BeiJing";
owner.Country = "China";
owner.State = "BeiJing";
owner.ImageName = "wo.jpg";
owner.LastActivityAmount = 100;
owner.LastActivityDate = DateTime.Today;
owner.CurrentBalance = 1234.56;
owner.BirthDate = new DateTime(1986, 6, 8);
owner.CustomerSince = new DateTime(2009, 12, 20);
}
到此,已经通过使用扩展标识语法(Markup Extension)实现了绑定到TextBlock的Text属性。就差最后一步,将Owner类实例绑定到控件,由下面代码来实现:
public MainPage()
{
InitializeComponent();
InitializeOwner();
OwnerDetailsGrid.DataContext = owner;
}
效果如下:
回过头来再看XAML代码,上面说过,这儿是通过扩展标识实现TextBlock的数据绑定:
<TextBlock Text="{Binding CustomerSince}" />
实际上这是一种缩写写法,完整的绑定语法如下:
<TargetControl TargetProperty="{Binding SourceProperty,SomeBindingProperties}" />
Class Owner
public class Owner
{
public int OwnerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string ZipCode { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
public DateTime BirthDate { get; set; }
public DateTime CustomerSince { get; set; }
public string ImageName { get; set; }
public DateTime LastActivityDate { get; set; }
public double CurrentBalance { get; set; }
public double LastActivityAmount { get; set; }
}
然后在MainPage.xaml.cs中创建一个Owner类的实例
MainPage.xaml.cs
public MainPage()
{
InitializeComponent();
InitializeOwner();
}
private void InitializeOwner()
{
owner = new Owner();
owner.OwnerId = 1234567;
owner.FirstName = "Zhu";
owner.LastName = "jz";
owner.Address = "ChaoYang BJ";
owner.ZipCode = "100001";
owner.City = "BeiJing";
owner.Country = "China";
owner.State = "BeiJing";
owner.ImageName = "wo.jpg";
owner.LastActivityAmount = 100;
owner.LastActivityDate = DateTime.Today;
owner.CurrentBalance = 1234.56;
owner.BirthDate = new DateTime(1986, 6, 8);
owner.CustomerSince = new DateTime(2009, 12, 20);
}
到此,已经通过使用扩展标识语法(Markup Extension)实现了绑定到TextBlock的Text属性。就差最后一步,将Owner类实例绑定到控件,由下面代码来实现:
public MainPage()
{
InitializeComponent();
InitializeOwner();
OwnerDetailsGrid.DataContext = owner;
}
效果如下:
回过头来再看XAML代码,上面说过,这儿是通过扩展标识实现TextBlock的数据绑定:
<TextBlock Text="{Binding CustomerSince}" />
实际上这是一种缩写写法,完整的绑定语法如下:
<TargetControl TargetProperty="{Binding SourceProperty,SomeBindingProperties}" />
相关文章推荐
- silverlight 数据绑定简单示例(creating dynamic bindings)
- Silverlight学习之【最简单数据绑定示例】
- Flex4 数据绑定简单示例
- Silverlight之MVVM模式简单介绍项目(数据绑定)
- Silverlight数据绑定转换示例
- Silverlight数据绑定简单例子
- 【译文】详解Data Binding - 通过几个简单示例深入了解WinForm数据绑定特性
- Silverlight数据绑定转换示例
- 【万里征程——Windows App开发】数据绑定——简单示例、更改通知、数据转换
- Silverlight 之数据绑定(简单例子)
- Silverlight数据绑定转换示例
- SilverLight中datagrid数据绑定的简单使用
- silverlight简单数据绑定2
- 【万里征程——Windows App开发】数据绑定——简单示例、更改通知、数据转换
- Silverlight 3 DataGrid简单数据绑定
- 【转】详解Data Binding 通过几个简单示例深入了解WinForm数据绑定特性
- 详解Data Binding - 通过几个简单示例深入了解WinForm数据绑定特性
- silverlight简单数据绑定1
- silverlight简单数据绑定3
- Silverlight 数据显示和布局控件 示例