2015-02-06 72 views
-2

在我的代碼隱藏中,如何編輯ListView中的值?
我有這樣的:如何編輯ListView中的項目?

<ListView x:Name="EntreesListView"> 
    <ListView.View> 
    <GridView> 
     <GridViewColumn Header="Entrees" 
         Width="Auto" 
         DisplayMemberBinding="{Binding Name}"/> 

這:

// Select all data in my database and add them into my column 
private void ListViewAllEntrees() 
{ 
    using (var selectAllEntrees = new Database1Entities()) 
    { 
     var selectName = 
      from monEntreeList 
      in selectAllEntrees.Entrees 
      select monEntreeList; 
      foreach (var entree in selectName) 
      { 
       EntreesListView.Items.Add(new {entree.Name}); 
      } 
// Now I want to edit for example the value at index 5 
// I click on a button which is in my ListView too 
var item = (sender as FrameworkElement).DataContext; 
int indexEntrees = EntreesListView.Items.IndexOf(item); 
// Now I have my index 
// I can RemoveAt(indexEntrees) and make a new Add but it is not a solution 

所以,我該怎麼辦,在指數5編輯值名稱嗎?

+0

NO元素的索引。子項在WPF中不存在。上。這不是重複。請在閱讀之前閱讀。 – C0ZEN 2015-02-06 08:32:19

回答

1

有幾種方法可以完成這項任務。

而不是添加列表中的每個項目。您可以使用

EntreesListView.ItemSource = selectName; 
EntreesListView.Refresh(); 

現在你的答案,如何獲得或修改某些事件或5的任何元素 當你得到它被點擊

//get the item from original list 
var obj = selectName[index]; 
//make changes to your object and again change the ItemSource 
EntreesListView.ItemSource = selectName; 
EntreesListView.Refresh(); 
+0

非常感謝,我會試試這個。 – C0ZEN 2015-02-06 10:51:24