windows phone 之ListBox数据绑定

对于windows phone的ItemSource绑定,需要注意,绑定的数据源必须要声明get,否则绑定不生效

 1  <ListBox ItemsSource="{ Binding  Students }">

 2                 <ListBox.ItemTemplate>

 3                     <DataTemplate>

 4                         <StackPanel Orientation="Vertical">

 5                             <TextBlock Text="学号:" Foreground="Blue"></TextBlock>

 6                             <TextBlock Text="{Binding StuId}"></TextBlock>

 7                             <TextBlock Text="姓名:" Foreground="Blue"></TextBlock>

 8                             <TextBlock Text="{Binding Name}"></TextBlock>

 9                             <TextBlock Text="年龄:" Foreground="Blue"></TextBlock>

10                             <TextBlock Text="{Binding Age}"></TextBlock>

11                             <TextBlock Text="家庭住址:" Foreground="Blue"></TextBlock>

12                             <TextBlock Text="{Binding Address}"></TextBlock>

13                             <TextBlock Text="-----------------------"></TextBlock>

14                         </StackPanel>

15                     </DataTemplate>

16                 </ListBox.ItemTemplate>

17             </ListBox>
 1   public List<Student> _Students = null;

 2         public List<Student> Students { get { return _Students; } }//必须要有get访问器

 3 

 4         // 构造函数

 5         public MainPage()

 6         {

 7            

 8             if (_Students == null)

 9             {

10                 _Students = new List<Student>();

11                 _Students.Add(new Student() { StuId = "001", Name = "张一", Age = 12, Address = "杭州市滨江区江南大道1" });

12                 _Students.Add(new Student() { StuId = "002", Name = "张二", Age = 12, Address = "杭州市滨江区江南大道2" });

13                 _Students.Add(new Student() { StuId = "003", Name = "张三", Age = 12, Address = "杭州市滨江区江南大道3" });

14                 _Students.Add(new Student() { StuId = "004", Name = "张四", Age = 12, Address = "杭州市滨江区江南大道4" });

15                 _Students.Add(new Student() { StuId = "005", Name = "张五", Age = 12, Address = "杭州市滨江区江南大道5" });

16 

17             }

18             InitializeComponent();

19 

20         }

21 

22 

23         protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)

24         {

25             DataContext = this;

26             base.OnNavigatedTo(e);

27         }
1  public class Student

2     {

3         public string StuId { get; set; }

4         public string Name { get; set; }

5         public int Age { get; set; }

6         public string Address { get; set; }

7 

8     }

 

你可能感兴趣的:(windows phone)