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;}

}

 

运行——————》

        

posted on 2011-11-14 14:09  Games  阅读(267)  评论(0编辑  收藏  举报