Data Binding(1)
时间:2010-12-14 来源:小楼①夜听春雨
1.
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock Name="myText" Height="50" Foreground="LightBlue" Text="{Binding}" Margin="57,125,65,125"></TextBlock>
</Grid>
2.
public class Employee
{
public String Name { set; get; }
public DateTime BirthDay { set; get; }
public Byte Gender { set; get; }
public override string ToString()
{
return String.Format("Name:{0}, Birth of Day :{1}, Gender:{2}",Name, BirthDay.ToLongDateString(), Gender == 1? "Male":"Female");
}
}
3.
public partial class MainPage : UserControl
{
public Employee e = new Employee() { BirthDay = new DateTime(1984, 06, 28), Gender = 1, Name = "Mo.Li" };
public MainPage()
{
InitializeComponent();
this.myText.DataContext = e;//相应控件的DataContext 引用CLR对象
}
}
binding 到这个对象了, 很显然textblock 默认会调用ToString方法,开始so easy !
相关阅读 更多 +
排行榜 更多 +










