WPF如何绑定gridview?

WPF如何绑定gridview?,wpf,binding,Wpf,Binding,我正在做这个xaml: <StackPanel Margin="320,0,0,0" Grid.RowSpan="2"> <ListView ItemsSource="{Binding employeeCollection}"> <ListView.View> <GridView> <GridViewColumn Header=

我正在做这个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; }
}
class employeesGrid/:INotifyPropertyChanged
{
ObservableCollection\u employeeCollection=
新的可观察集合();
公共雇员网格()
{
_employeeCollection.Add(新员工数据{
EmployeeID=“魔兽世界”,
FirstName=“暴雪”,
LastName=“暴雪”,
startHR=“2222”,
finishHR=“dfs”
});
}
公共可见集合雇员集合
{get{return}employeeCollection;}
}
公共类EmployeesData
{
公共字符串EmployeeID{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共字符串startHR{get;set;}
公共字符串finishHR{get;set;}
}
}

在我的主窗口中,我正在执行以下操作:

//建造商: 初始化组件(); employeesGrid em=新employeesGrid()

有人能告诉我我做错了什么吗? 2.InotifyProperty已更改我为什么要使用它?我应该如何使用它

感谢你关注我的工作,这对我来说意义重大:)


假设我希望在我的程序中有两个这样的结构,这是最好的实现方式???

您从未设置ListView的数据上下文

在窗口构造函数中尝试以下操作:

InitializeComponent(); 
employeesGrid em = new employeesGrid();
this.DataContext = em;
  • 您需要将视图的数据源绑定到类实例。在构造函数中,执行以下操作:
    this.DataContext=newEmployeesGrid()
  • INotifyPropertyChanged是一个界面,如果希望UI在基础内容更改时刷新其内容,则应使用该界面

  • 我已经找到了soultion thankuyes问题是DataContext=vm//vm是类ConnectionViewModel:INotifyPropertyChanged所以我如何让他们生活在一起什么应该是archtcture?@yoav.str,employeeCollection属性应该通过ConnectionViewModel类公开。在getter中,如果必须保留两个类而不是将它们合并为一个类,则只需返回employeesGrid.employeeCollection即可。mmm意味着connectionHandler hm谁支持集合数?有更好的结构吗?或者举个例子,如何尽可能地简单/专业?