2013-12-17 3 views
4

LstbClients라는 ListBox에서 각 Label 또는 TextBlock에 Name 및 Phonenumber를 표시하려면 (필자에게 상관 없음) 이전에 ComboBox를 사용하여 DataBinded를 사용하고 작업했는지 확인합니다 꽤 좋긴하지만 어떤 이유로이 ListBox에서 작동하지 않습니다.목록 상자에 특정 개체의 값이 표시되지 않음 (데이터 바인딩)

이것은 XAML 코드입니다. 이 목록 상자의 레이블 만 보여 않습니다 어떤 이상한 이유로

public class Client 
{ 
    public int ID; 
    public string Lastname; 
    public string Name; 
    public string Streetname; 
    public string Postcode; 
    public string City; 
    public int Phonenumber; 
    public string Email; 
    public DateTime CreationDate; 
    public DateTime BirthDate; 

    public Client(int id, string lastname, string name, DateTime birthDate, string streetname, string postcode, string city, int phonenumber, string email, DateTime creationDate) 
    { 
     this.ID = id; 
     this.Lastname = lastname; 
     this.Name = name; 
     this.Streetname = streetname; 
     this.Postcode = postcode; 
     this.City = city; 
     this.Phonenumber = phonenumber; 
     this.Email = email; 
     this.CreationDate = creationDate; 
     this.BirthDate = birthDate; 
    } 
} 

Client.cs

뒤에 코드

//clients is a filled ObservableCollection<client> 
lstbClients.ItemsSource = clients; 

":

<ListBox x:Name="lstbClients" 
     Height="300" 
     Grid.Row="0" Grid.Column="0" 
     Style="{StaticResource inputControls}" 
     ItemsSource="{Binding clients}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <Label Content="{Binding Name}"/> 
       <Label Content=", "/> 
       <Label Content="{Binding Phonenumber}"/> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

이 뒤에 코드는 "이름과 전화 번호를 무시하고 예를 들어 WPF 응용 프로그램에서 ListBox를 열면 모든 데이터에 ... ListBox가 데이터를 가져 오지만 레이블에 표시하지 않으려 고합니다. lstbClients, 그래서 어떤 레이블에 어떤 데이터가 포함되어 있는지 확인할 수는 없습니다.

+0

클라이언트 클래스의 코드를 보여줍니다. 왜 XAML과 코드 숨김에서 ItemsSource를 설정하고 있습니까? –

+5

사이트에 오신 것을 환영합니다! 귀하의 질문에 "나는 초학자입니다."라고 덧붙일 필요가 없습니다. 모두가 초보자 였으므로 걱정하지 마십시오. 프로젝트에 행운을 빌어 요! – dasblinkenlight

+0

XAML이 훌륭하게 보입니다. Observable Collection에 INotifyPropertyChanged를 구현하지 않았다고 추측합니다. Observable Collection 선언을위한 코드를 보여주십시오. 또한 런타임 중에 출력 창을 검사하여 바인딩 오류가 발생하지 않도록하십시오. 그것들은 당신이 당신의 문제를 찾도록 도울 수 있습니다. –

답변

3

바인딩은 필드가 아닌 속성과 함께 작동합니다.

은 다음과 같이 속성에 필드를 변경

:

public string Name {get; set;} 
public int Phonenumber {get; set;} 

을 (!) 참고에 당신이 경우에 당신의 클래스에 대한 INotifyPropertyChanged를 구현해야 당신이 GUI는 모든 속성 변화에 새로 고침되고 싶어요. 이에 대한 참조는 How to implement Property change notification입니다.

+0

고마워요! 이 작품은, 나는 항상 얻고 설정하는 것이 필요하지 않을 때 논리를 넣어 ...하지만 내가 틀렸어 보인다. 어쨌든,이 답변을 주셔서 감사 드리며, 또한 INotifyPropertyChanged에 대해 더 자세히 살펴 보겠습니다. –

+0

이들은 자동 속성입니다. 컴파일러가 자동으로 백업 필드를 만듭니다. –

관련 문제