您的位置:首页 > 其它

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}" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: