2

我在尝试将列表绑定到列表视图时遇到问题,我得到一个空列表视图。我已经尝试将我的列表更改为一个属性,因为我看到这解决了其他人的问题,但没有运气,任何帮助表示赞赏。

这是 XAML

<Window x:Class="key_stage_level_2_app.Window7"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 DataContext="{Binding RelativeSource={RelativeSource Self}}"
 Title="Window7" Height="600" Width="800" Loaded="Window_Loaded">

<Grid>
    <TabControl Height="536" HorizontalAlignment="Left" Name="tabControl1"      VerticalAlignment="Top" Width="778">
        <TabItem Header="Results2" Name="tabItem2">
            <Grid>
                <ListView Height="323" HorizontalAlignment="Left" Margin="107,96,0,0" Name="listView2" VerticalAlignment="Top" Width="186" ItemsSource="{Binding Path=someList}" >
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Width="80"  Header="Name" DisplayMemberBinding="{Binding Path=Name}"/>
                            <GridViewColumn Width="80 " Header="Result" DisplayMemberBinding="{Binding Path=Result}"/>    
                        </GridView>   
                    </ListView.View>
                </ListView>

这是后面的代码

namespace key_stage_level_2_app
{
/// <summary>
/// Interaction logic for Window7.xaml
/// </summary>
public partial class Window7 : Window
{

    App app = (App)App.Current;
    private List<Pupil> someList { get; set; }


    public Window7()
    {
        someList = new List<Pupil>();

        foreach (Pupil pupil in app.PupilList)
        {
            someList.Add(pupil);

        }
        someList.Add(new Pupil
        {
            Name = "Simon",
            Result = 100,
            Grade = 100.00,
            ExtensionWork = true,
            TakenTest = true

        });
        Console.WriteLine("someList size = " + someList.Count);

        InitializeComponent();

    }

和班级

class Pupil
{

    public String name;
    int result;
    double grade;
    bool extensionWork;
    bool takenTest;

    public Pupil(String Pname, int Presult, double Pgrade, bool PextensionWork, bool PtakenTest)
    {
        Name = Pname;
        Result = Presult;
        Grade = Pgrade;
        ExtensionWork = PextensionWork;
        TakenTest = PtakenTest;

    }
    public Pupil()
    {

    }

    public String Name
    {
        get { return name; }
        set { name = value; }
    }


    public int Result
    {
        get { return result; }
        set { result = value; }
    }


    public double Grade
    {
        get { return grade; }
        set { grade = value; }
    }


    public bool ExtensionWork
    {
        get { return extensionWork; }
        set { extensionWork = value; }
    }


    public bool TakenTest
    {
        get { return takenTest; }
        set { takenTest = value; }
    }
4

3 回答 3

1

您什么也得不到,因为您正在创建新列表,但您的列表为空,请执行以下操作:

someList = new List<Pupil>();

someList.Add(new Pupil
        {
            Name = "Simon",
            Result = 100,
            Grade = 100.00,
            ExtensionWork = true,
            TakenTest = true

        });

        foreach (Pupil pupil in app.PupilList)
        {
            someList.Add(pupil);

        }
于 2012-04-26T13:49:36.890 回答
1

在初始化组件()之后;添加以下行:

DataContext = this;

以便 View 知道从哪里获取 someList。

此外,Window7 和 Pupil 都应实现 INotifyPropertyChanged 接口以允许更新并避免内存泄漏。

于 2012-04-26T13:49:38.317 回答
1

将您的类访问修饰符更改为:

public class Pupil

并使 someList 属性公开:

public List<Pupil> SomeList { get; set; }

所以视图实际上可以看到它。

于 2012-04-26T14:29:31.787 回答