如何在ListView中编辑项目?

时间:2015-02-06 08:13:11

标签: c# wpf xaml listview listviewitem

在我的代码隐藏中,如何编辑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处编辑值名称?

1 个答案:

答案 0 :(得分: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();