[英]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.