2010-08-21 72 views
1

我這樣做XAML:WPF如何綁定gridview?

<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.can有人請指導我我做錯了什麼? 2.INotifyPropertyChanged我爲什麼要使用它?我應該如何使用它?

thanku對我的工作凝視它意味着很多我:)

可以說,我想要兩個sturctures像這樣在我的計劃什麼是最好的implmantion ????

+0

我媒體鏈接發現soultion thanku – 2010-08-21 22:22:09

回答

1

您從未設置過您的列表視圖'DataContext。

試試這個在窗口構造函數:

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

是問題的DataContext = VM // VM是類ConnectionViewModel:INotifyPropertyChanged的 所以我怎麼讓他們住togther應該是什麼archtcture? – 2010-08-21 22:35:36

+0

@ yoav.str,employeeCollection屬性應該通過ConnectionViewModel類公開。在getter中,如果必須保留兩個類,而不是將它們合併爲一個,則可以簡單地返回employeesGrid.employeeCollection。 – 2010-08-21 22:55:50

+0

mmm含義connectionHandler hm誰支持收集數量?有沒有更好的archticture?或任何例子如何做到這一點簡單/贊成它? – 2010-08-21 23:33:16

1
  1. 您需要將您的視圖的數據源綁定到你的類的實例。在您的構造函數中,執行以下操作:this.DataContext = new employeesGrid();
  2. INotifyPropertyChanged是一個接口,如果您希望UI在基礎內容更改時刷新其內容,則應使用該接口。