2013-02-15 61 views
2

我有我正在使用測試,如何輸入到列表中的以下代碼。這些項目出現在列表中,但未顯示。Windows Phone列表不顯示數據

public MyFellows() 
    { 
     InitializeComponent(); 
     List<Fellow> fellowList = new List<Fellow>(); 
     for (int i = 0; i < 2; i++) 
     { 
      Fellow fellow = new Fellow(); 
      fellow.Name = "Danish " + i; 
      fellow.Email = "Email " + i; 
      fellowList.Add(fellow); 
     } 
     lbFellows.ItemsSource = null; 
     lbFellows.ItemsSource = fellowList; 
    } 

    private class Fellow 
    { 
     public string Name { get; set; } 
     public string Email { get; set; } 
    } 

和下面的XAML正在使用

<ListBox x:Name="lbFellows" Margin="8,8,8,177"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Vertical" Width="Auto" Height="300"> 
         <TextBlock x:Name="tbName" Width="Auto" FontSize="22" FontWeight="Bold" Text="{Binding Name}" /> 
         <TextBlock x:Name="tbEmail" Width="Auto" FontSize="22" Height="Auto" Text="{Binding Email}"/> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 

回答

1

所有WPF首先僅結合性質和屬性必須在對象的DataContext的存在。

您必須改變這樣的代碼:

public List<Fellow> fellowList { get; set; } 
// Constructor 
public MainPage() 
{ 
    InitializeComponent(); 

    fellowList = new List<Fellow>(); 
    for (int i = 0; i < 2; i++) 
    { 
     Fellow fellow = new Fellow(); 
     fellow.Name = "Danish " + i; 
     fellow.Email = "Email " + i; 
     fellowList.Add(fellow); 
    } 
    this.DataContext = this; 
    //lbFellows.ItemsSource = null; 
    lbFellows.ItemsSource = fellowList; 
} 

public class Fellow 
{ 
    public string Name { get; set; } 
    public string Email { get; set; } 
} 
+0

使得研究員類公共工程,是有必要定義列表作爲財產? – mdanishs 2013-02-15 18:24:58