绑定列表视图不绑定

本文关键字:绑定 列表 视图 | 更新日期: 2023-09-27 18:32:16

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

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

绑定列表视图不绑定

将类访问修饰符更改为:

public class Pupil

并使 someList 属性公开:

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

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

由于您正在创建新列表但列表为空,因此您一无所获,请执行以下操作:

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);
        }

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

DataContext = this;

以便视图知道从哪里获取 someList。

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