简体   繁体   English

如何从Xamarin Forms ListView中删除项目?

[英]How to remove an item from a Xamarin Forms ListView?

I'm trying to remove items/rows from a ListView but the difficulty is that I need to also pass in some delegate or fire some event or something, so when a person clicks a button to remove that row, my code handles some other logic, elsewhere (eg. remove the item from the DB or whatever). 我正在尝试从ListView删除项目/行,但困难在于我还需要传递一些委托或引发某些事件或某些事情,因此当某人单击按钮以删除该行时,我的代码将处理其他逻辑,其他位置(例如,从数据库中删除该项目或其他内容)。

I have a custom control I made: 我有一个自定义控件:

public class SportsTeam : StackLayout { .. }

Inside this control, one of the elements is a ListView which lists all the people in a sporting team. 在此控件内部,其中一个元素是ListView ,其中列出了运动队中的所有人员。

var viewModel = teamMembers.Select(x => new SportsTeamViewModel(x));

return new ListView
{
    HasUnevenRows = true,
    ItemSource = viewModel,
    ItemTemplate = new DataTemplate(typeof(SportsTeamViewCell)); 
};

Inside the SportsTeamViewCell I have the following: SportsTeamViewCell我具有以下内容:

private Grid CreateContent()
{
    var grid = new Grid();
    // Setup row and column definitions.
    // Add items to the Grid 
    grid.Children.Add(...);

    var removeButton = RemoveButton;
    grid.Children.Add(removeButton);
    Grid.SetRowSpan(removeButton, 2);

    return grid;
}

private Button RemoveButton
{
    get
    {
        var button = new Button
        {
            Image = "Icons/remove.png"
        };

        return button;
    }
}

From here, I don't know how to make it so that the button fires an event or some delete could be passed in via the constructor, so some custom logic is performed against the individual cell/row/item that is to be removed. 从这里开始,我不知道如何使按钮触发事件或可以通过构造函数传递一些删除,因此对要删除的单个单元格/行/项目执行了一些自定义逻辑。

Here is what you could do : 您可以执行以下操作:

This be my model class : 这是我的模型课:

public class Item  
{  
   public string ItemName { get; set; }  
   public string ItemDetails { get; set; }  
}  

And in my XAML or you can write this in code as well, bind to the Command Parameter of your Item template : 并且在我的XAML中,或者您也可以用代码编写,将其绑定到Item模板的Command Parameter

<Button Text="Delete" CommandParameter="{Binding ItemName}" Clicked="DeleteClicked"></Button>

Full Item Template will be like below : 完整项目模板如下所示:

<ListView.ItemTemplate>  
            <DataTemplate>  
               <ViewCell>  
                  <ViewCell.View>  
                     <StackLayout Orientation="Horizontal">  
                        <Label Text="{Binding ItemName}" HorizontalOptions="StartAndExpand" FontSize="30"></Label>  
                        <Button Text="Delete" CommandParameter="{Binding ItemName}" Clicked="DeleteClicked">        
                        </Button>  
                     </StackLayout>  
                  </ViewCell.View>  
               </ViewCell>  
            </DataTemplate>  
         </ListView.ItemTemplate>    

And in you code file you can do this : 在您的代码文件中,您可以执行以下操作:

public void DeleteClicked(object sender, EventArgs e)  
{  
   var item = (Xamarin.Forms.Button)sender;  
   Item listitem = (from itm in allItems 
                    where itm.ItemName == item.CommandParameter.ToString() 
                    select itm)
                   .FirstOrDefault<Item>();  
   allItems.Remove(listitem);  
}  

IMPORTANT : This would only delete the item from the bound collection. 重要说明 :这只会从绑定的集合中删除该项目。 To delete it from the original list you need to use ObservableCollection 要将其从原始列表中删除,您需要使用ObservableCollection

Here is the full source code of the explained scenario - Handling Child Control Event in Listview using XAMARIN.FORMS . 这是说明的方案的完整源代码- 使用XAMARIN.FORMS在Listview中处理子控件事件

Also the Tutorial - How to handle Row selection and delete Button in Row For Custom ListView using Xamarin.Forms explain deletion from a listview as well. 另外,教程- 如何使用Xamarin.Forms处理自定义ListView的行中的行选择和删除按钮表单还说明了从listview删除。

I've found a similar approach and I want to share it. 我找到了一种类似的方法,我想分享一下。 I filled the list with an ObservableCollection<MyObject> . 我用ObservableCollection<MyObject>填充了列表。 Then I filled the CommandParameter with just CommandParameter="{Binding .}" . 然后我只用CommandParameter="{Binding .}"填充CommandParameter。 So I got the whole Object back. 所以我拿回了整个物件。 Then you can just cast the CommandParameter to your Object and remove it from the ObservableCollection<MyObject> List 然后,您可以将CommandParameter为对象,然后将其从ObservableCollection<MyObject>列表中删除。

XAML: XAML:

CommandParameter="{Binding .}"

Filling my List: 填写我的清单:

savingExpensesCollection = new ObservableCollection<SavingsExpensesEntry> ();
savingExpensesCollection .Add (new SavingsExpensesEntry ("1000 mAh Akku", "Dampfgarten", new DateTime (635808692400000000), 8.95));
savingExpensesCollection .Add (new SavingsExpensesEntry ("Cool-Mint Aroma", "Dampfgarten", new DateTime (635808692400000000), 3.95));
savingExpensesCollection .Add (new SavingsExpensesEntry ("Basis", "Dampfgarten", new DateTime (635808692400000000), 13.65));

savingExpensesList.ItemsSource = savingExpenses;

EventHandler: 事件处理程序:

void OnDelete(object sender, EventArgs e)
{
    var menuItem = ((MenuItem)sender);
    SavingsExpensesEntry see ((SavingsExpensesEntry)menuItem.CommandParameter);
    savingExpensesCollection .Remove (see);
}

I've using a MenuItem but it's the same approach with a Button 我使用了MenuItem但使用Button方法相同

I just did using delete button 我只是使用删除按钮

  public void OnDelete(object sender, EventArgs e)
    {
        var mi = ((MenuItem)sender);

        PhotoViewModel photo= ((photoViewModel)mi.CommandParameter);
        photoModel.Remove(photo);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 xamarin forms 中的列表视图中获取所选项目索引? - How to get the selected item index from the listview in xamarin forms? 如何从 Xamarin Forms 中的自定义 ViewCell 获取 ListView 项目索引? - How to get ListView item index from custom ViewCell in Xamarin Forms? Xamarin Forms,如何使用 ListView 收藏项目 - Xamarin Forms, How to favorite Item using ListView C#-Xamarin表单在切换时删除列表视图项 - C# - Xamarin forms remove listview item on toggle Xamarin.Forms - 如何使用 ObservableCollection 从包含 ListView 中选择项目 - Xamarin.Forms - How to select item from its containing ListView with ObservableCollection 如何通过打开上下文操作菜单从ListView中获取所选项目? (Xamarin.Forms) - How do I get the selected item from a ListView by opening the context actions menu? (Xamarin.Forms) 如何为 xamarin 表单中的每个列表视图项提供不同的文本颜色 - how to give different text color for each item of listview in xamarin forms 如何在列表视图的项目中引用控件或视图-Xamarin Forms - How do I refer a control or view in an item of listview - xamarin forms Xamarin Forms - 单击图像时如何选择 ListView 项? - Xamarin Forms - How to select a ListView item when its image is clicked? 如何使用 XAML 在 Xamarin.Forms 的 ListView 中选择一个项目 - How can I select an item in a ListView in Xamarin.Forms with XAML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM