【问题标题】:Binding ListView not Binding绑定 ListView 不绑定
【发布时间】:2012-05-07 06:20:33
【问题描述】:

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

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

【问题讨论】:

    标签: c# wpf listview binding


    【解决方案1】:

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

    public class Pupil
    

    并公开 someList 属性:

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • Console.WriteLine("someList size = " + someList.Count); is == 2 列表不为空,谢谢
      • 该列表包含从另一个窗口添加的学生,并且我在此窗口中添加了一个新学生以确保列表不为空。
      • foreach 循环正在迭代一个不同的列表,它将瞳孔添加到包含一个瞳孔的列表中,然后我创建一个新瞳孔并将其添加到列表中。控制台的输出是 2。
      【解决方案3】:

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

      DataContext = this;
      

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

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

      【讨论】:

      • 我以前试过这个,但没有运气。我在 XAML DataContext="{Binding RelativeSource={RelativeSource Self}}" 中有这个,所以不需要它。还是谢谢
      猜你喜欢
      • 2023-04-10
      • 2011-08-30
      • 1970-01-01
      • 1970-01-01
      • 2014-01-14
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 2014-10-31
      相关资源
      最近更新 更多