2010-08-25 4 views
0

J'ai classe Employé qui est quelque chose comme ça.Comment puis-je cataloguer une collection dans une zone de liste dans WPF?

class Emp 
     { 
      int EmpID; 
      string EmpName; 
      string ProjectName; 
     } 

J'ai un list<employee> empList peuplé et veulent le montrer dans la zone de liste. Je définissez la propriété ItemSource de la liste déroulante située à empList et mon code XAML est le suivant

<ListBox Name="lbEmpList"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel> 
         <Label Content="{Binding Path=EmpID}"></Label> 
         <Label Content="{Binding Path=EmpName}"></Label> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 

Inutile de dire que cela ne fonctionne pas ... tous les pointeurs à ce que je fais mal serait utile .. merci d'avance

Répondre

1

Codebehind:

public partial class Window1 : Window, INotifyPropertyChanged 
{ 
    private List<Emp> _empList = new List<Emp>(); 
    public Window1() 
    { 
     EmpList.Add(new Emp {EmpID = 1, EmpName = "John", ProjectName = "Templates"}); 
     EmpList.Add(new Emp { EmpID = 2, EmpName = "Smith", ProjectName = "Templates" }); 
     EmpList.Add(new Emp { EmpID = 3, EmpName = "Rob", ProjectName = "Projects" }); 

     InitializeComponent(); 
     lbEmpList.DataContext = this; 
    } 


    public List<Emp> EmpList 
    { 
     get { return _empList; } 
     set 
     { 
      _empList = value; 
      raiseOnPropertyChanged("EmpList"); 
     } 
    } 

    #region Implementation of INotifyPropertyChanged 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void raiseOnPropertyChanged (string propertyName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 

    #endregion 
} 

public class Emp 
{ 
    public int EmpID { get; set;} 
    public string EmpName { get; set; } 
    public string ProjectName { get; set; } 
} 

XAML:

<Grid x:Name="grid" ShowGridLines="True"> 
    <ListBox Name="lbEmpList" ItemsSource="{Binding EmpList}"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel> 
        <Label Content="{Binding Path=EmpID}"></Label> 
        <Label Content="{Binding Path=EmpName}"></Label> 
       </StackPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
</Grid> 

fonctionne comme prévu. J'espère que cela t'aides.

+0

cela a fonctionné .. merci beaucoup –

0

Rendre les champs public properties et implémenter INotifyPropertyChanged. Vous pouvez également avoir un ObservableCollection au lieu d'une liste:

class Emp :INotifyPropertyChanged 
{ 
    int _empId; 
    public int EmpId 
    { 
    get { return _empId; } 
    set { _empId = value; NotifyChanged("EmpId"); } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    public void NotifyChanged(string property) 
    { 
    if (PropertyChanged != null) 
     PropertyChanged(this, new PropertyChangedEventArgs(property)); 
    } 
} 
+0

J'ai trouvé beaucoup d'exemples avec ObservableCollection sur le net ... mais ce que je voulais savoir est-ce possible avec la liste? et oui tous les membres de la classe sont publics je voulais juste donner une idée ... désolé abt que –

Questions connexes