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