绑定ListView不绑定

时间:2012-04-26 13:44:17

标签: c# wpf listview binding

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

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

3 个答案:

答案 0 :(得分: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);

        }

答案 1 :(得分:1)

在InitializeComponent()之后;添加以下行:

DataContext = this;

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

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

答案 2 :(得分:1)

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

public class Pupil

并将someList属性设为public:

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

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