Windows Phone列表未显示数据

本文关键字:显示 数据 列表 Phone Windows | 更新日期: 2023-09-27 17:59:28

我有以下代码,我正在使用这些代码来测试如何输入到列表中。这些项目存在于列表中,但未显示。

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>

Windows Phone列表未显示数据

首先,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; }
}