【问题标题】:ListBox items not showing列表框项目未显示
【发布时间】:2016-06-27 09:42:30
【问题描述】:

所以我编写了一个简单的数据绑定实践项目,但是我有一个小问题。我有一个 ObservableCollection,我将其数据绑定到 ListBox。当我运行程序时,起初似乎 ListBox 中没有任何内容,但是当我将鼠标悬停在它上面时,我可以看到与 ObservableCollections 中完全相同数量的项目的轮廓,但没有任何内容说明它们。 这是我的代码:

XAML:

 <ListBox Margin="0, 40, 0, 0" x:Name="lb1" ScrollViewer.CanContentScroll="True">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <DockPanel>
                    <TextBlock DockPanel.Dock="Left" x:Name="_age" Text="{Binding Path=Age}"/>
                    <TextBlock DockPanel.Dock="Right" x:Name="_cash" Text="{Binding Path=Cash}" />
                    <TextBlock DockPanel.Dock="Top" x:Name="_name" Text="{Binding Path=FullName}"/>
                    <TextBlock DockPanel.Dock="Top" x:Name="_gender" Text="{Binding Path=Gender}"/>
                </DockPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

代码隐藏:

public class Person{
    public Person(string _FullName, int _Age, string _Gender, string _AboutMe, decimal _Cash)
    {
        this.FullName = _FullName;
        this.Age = _Age;
        this.Gender = _Gender;
        this.AboutMe = _AboutMe;
        this.Cash = _Cash;

    }
    public string FullName;
    public int Age;
    public string Gender;
    public string AboutMe;
    public decimal Cash;
}

public partial class MainWindow : Window
{

   public ObservableCollection<Person>People = new ObservableCollection<Person>();

    public MainWindow()
    {
        People.Add(new Person("John Doe", 35, "M", "My name is John Doe", 1500));
        People.Add(new Person("Sarah Maine", 28, "F", "My name is Sarah Maine", 2150));
        People.Add(new Person("George Smith", 65, "M", "My name is George Smith", 4999));
        People.Add(new Person("Rachel Ramsey", 46, "F", "My name is Rachel Ramsey", 3199));
        People.Add(new Person("Rachel Ramsey", 46, "F", "My name is Rachel Ramsey", 3199));
        InitializeComponent();

        lb1.ItemsSource = People;

    }
}

有什么想法吗?谢谢

【问题讨论】:

  • 需要是带有“get”的公共财产

标签: wpf data-binding


【解决方案1】:

您的 Person-Class 需要一些属性,而不仅仅是字段:

public class Person {
    public Person(string _FullName, int _Age, string _Gender, string _AboutMe, decimal _Cash) {
      this.FullName = _FullName;
      this.Age = _Age;
      this.Gender = _Gender;
      this.AboutMe = _AboutMe;
      this.Cash = _Cash;

    }
    private string fullName;
    private int age;
    private string gender;
    private string aboutMe;
    private decimal cash;

    public string FullName {
      get {
        return fullName;
      }

      set {
        this.fullName = value;
      }
    }

    public int Age {
      get {
        return age;
      }

      set {
        this.age = value;
      }
    }

    public string Gender {
      get {
        return gender;
      }

      set {
        this.gender = value;
      }
    }

    public string AboutMe {
      get {
        return aboutMe;
      }

      set {
        this.aboutMe = value;
      }
    }

    public decimal Cash {
      get {
        return cash;
      }

      set {
        this.cash = value;
      }
    }
  }

考虑也实现INotifyPropertyChanged

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    • 1970-01-01
    • 2011-03-15
    相关资源
    最近更新 更多