2016-06-27 4 views
-1

그래서 간단한 데이터 바인딩 연습 프로젝트를 코딩했지만 조금 문제가 있습니다. ListBox에 데이터 바인딩 된 ObservableCollection이 있습니다. 프로그램을 실행하면 처음에는 ListBox에 아무 것도없는 것 같지만 마우스를 가져 가면 ObservableCollections에서와 동일한 항목 수의 윤곽을 볼 수 있지만 아무것도 표시되지 않습니다. 다음은 내 코드입니다 :ListBox 항목이 표시되지 않습니다.

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

는 "수"로 공공 재산 할 필요가 INotifyPropertyChanged을 구현하기 위해 고려 – 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; 
     } 
    } 
    } 

너무

관련 문제