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

InitializeComponent();の後 次の行を追加します。

DataContext = this;

ビューが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 に答える