WPF如何绑定gridview?

时间:2010-08-21 22:08:22

标签: wpf binding

我正在做这个xaml:

 <StackPanel Margin="320,0,0,0" Grid.RowSpan="2">
        <ListView ItemsSource="{Binding employeeCollection}">
            <ListView.View>
                <GridView>

                    <GridViewColumn Header="Employee ID" DisplayMemberBinding="{Binding Path=EmployeeID}"/>
                    <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding Path=FirstName}"/>
                    <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding Path=LastName}"/>
                    <GridViewColumn Header="start" DisplayMemberBinding="{Binding Path=startHR}"/>
                    <GridViewColumn Header="finish" DisplayMemberBinding="{Binding Path=finishHR}">

                </GridViewColumn>
            </GridView>
    </ListView.View>

        </ListView>
    </StackPanel>

后面的代码是:

class employeesGrid //: INotifyPropertyChanged
{
    ObservableCollection<employiesData> _employeeCollection = 
    new ObservableCollection<employiesData>();

    public employeesGrid()
{
    _employeeCollection.Add(new employiesData{

      EmployeeID = "World Of Warcraft", 
      FirstName = "Blizzard", 
      LastName = "Blizzard",
      startHR = "2222",
      finishHR = "dfs"
  });


}

    public ObservableCollection<employiesData> employeeCollection
{ get { return _employeeCollection; } }


}

public class employiesData
{
    public string EmployeeID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string startHR { get; set; }
    public string finishHR { get; set; }
}

}

我在主窗口中的位置:

//构造:      的InitializeComponent();      employeesGrid em = new employeesGrid();

1.有人请指导我,我做错了什么? 2.INotifyPropertyChanged我为什么要使用它?我该怎么用呢?

感谢凝视我的工作对我来说意义重大:)

我想在我的计划中想要这样的两个结构什么是最好的implmantion ????

2 个答案:

答案 0 :(得分:1)

您永远不会将listviews设置为DataContext。

在窗口构造函数中尝试:

InitializeComponent(); 
employeesGrid em = new employeesGrid();
this.DataContext = em;

答案 1 :(得分:1)

  1. 您需要将视图的数据源绑定到类实例。在构造函数中,执行以下操作:this.DataContext = new employeesGrid();
  2. 如果您希望UI在基础内容发生变化时刷新内容,则应使用INotifyPropertyChanged。