C#语言使用Windows phone 中的数据数据绑定

 

public class Sample

{

void Initialize()

{

ListBox PersonListBox = new ListBox();

PersonListBox.Width = 480;   

PersonListBox.Height = 800;    

PersonListBox.ItemTemplate = GetDatatemplate();         

PersonListBox.ItemsSource = LoadPerson();

}

 

//返回绑定数据的自定义数据模板

private DataTemplate GetDatatemplate()        

{            

StringBuilder sb = new StringBuilder();            

sb.Append("<DataTemplate xmlns='http://schemas.microsoft.com/client/2007'>");            

sb.Append("<StackPanel Orientation='Horizontal'>");            

sb.Append("<TextBlock Text='{Binding Name}' Width='50' HorizontalAlignment='Left'/>");            

sb.Append("<TextBlock Text='{Binding Sex}' Width='50' HorizontalAlignment='Left'/>");            

sb.Append("<Button Content='{Binding Button}' Width='200' Height='70' HorizontalAlignment='Center'/>");            

sb.Append(" <Image Source='{Binding Image}' Width='50' Margin='100,0,0,0' HorizontalAlignment='Right'/>");            

sb.Append("</StackPanel>");            

sb.Append("</DataTemplate>");            

return (DataTemplate)XamlReader.Load(sb.ToString());        

/*            

    *需要注意的是:            

  * 1. XamlReader 位于命名空间 System.Windows.Markup 中                

  * 2. 导入的XAML格式字符串最上层只能包含一个对象                

  * 3. 必须和待导入的文件拥有相同的 xmlns                 

  * 4. 导入的XAML格式字符中的对象不能拥有 x:name 属性            

  * 5. XamlReader.Load()不接受事件处理程序。不允许设置事件            

*/

}

 

//返回数据源对象

 private List<PersonModel> LoadPerson()        

{            

  List<PersonModel> PersonColl = new List<PersonModel>();            

  for (int i = 0; i < 10; i++)            

  {                

  PersonColl.Add(new PersonModel(i.ToString(),i.ToString()));            

  }            

  return PersonColl;        

}

}

 

//数据模板

public class Mode;

{

  public Model(string name.string sex)

  {

      Name=name;

    Sex=sex;

    Button="Content";

    Image="Image.jpg";

  }

  public string Name{get;set;}

  public string Sex{get;set;}

  public string Button{get;set;}

  public string Image{get;set;}

}

 

运行——————》

        

转载于:https://www.cnblogs.com/mokey/archive/2011/11/14/2248320.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值