开发者

How do I databind a collection to a listbox in WPF?

开发者 https://www.devze.com 2023-01-13 05:09 出处:网络
I have class Employee which is something like this. class Emp { int EmpID; string EmpName; 开发者_如何学Cstring ProjectName;

I have class Employee which is something like this.

   class Emp
        {
            int EmpID;
            string EmpName;
       开发者_如何学C     string ProjectName;
        }

I have a list<employee> empList populated and want to show it inside the list box. I set the itemsource property of the listbox to the empList and my XAML code is as follows

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

needless to say this does not work...any pointers to as what i am doing wrong would be helpful.. thank you in advance


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>

Works as expected. Hope this helps.


Make the fields public properties and implement INotifyPropertyChanged. You might also want to have an ObservableCollection instead of a List:

 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

精彩评论

暂无评论...
验证码 换一张
取 消