2016-06-27 16 views
-1

シンプルなデータバインディングの練習プロジェクトをコーディングしましたが、少し問題があります。私はListBoxにデータバインドされたObservableCollectionを持っています。私がプログラムを実行すると、まず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; 

    } 
} 

任意のアイデア?おかげ

+0

を実装することを検討します – Paparazzi

答えて

0

は、あなたの個人クラスは、いくつかのプロパティとフィールドだけではないが必要です。

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すぎ