如何从 Xamarin Forms ListView 中删除项目? [英] How to remove an item from a Xamarin Forms ListView?

查看:47
本文介绍了如何从 Xamarin Forms ListView 中删除项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 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).

我有一个自定义控件:

public class SportsTeam : StackLayout { .. }

在这个控件中,其中一个元素是一个 ListView,它列出了一个运动队中的所有人.

Inside this control, one of the elements is a ListView which lists all the people in a sporting team.

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

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

SportsTeamViewCell 中,我有以下内容:

Inside the SportsTeamViewCell I have the following:

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.

推荐答案

你可以这样做:

这是我的模型类:

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

在我的 XAML 中,或者您也可以在代码中编写它,绑定到您的项目模板的 Command Parameter :

And in my XAML or you can write this in code as well, bind to the Command Parameter of your Item template :

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

完整的项目模板如下:

<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);  
}  

重要事项:这只会从绑定的集合中删除项目.要将其从原始列表中删除,您需要使用 ObservableCollection

IMPORTANT : This would only delete the item from the bound collection. To delete it from the original list you need to use ObservableCollection

这里是解释场景的完整源代码 - 使用 XAMARIN.FORMS 处理 Listview 中的子控件事件.

Here is the full source code of the explained scenario - Handling Child Control Event in Listview using XAMARIN.FORMS.

还有教程 - 如何使用 Xamarin.Forms 处理自定义 ListView 中的行选择和删除按钮 也解释了从 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 中删除项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆