【发布时间】:2021-10-27 00:18:39
【问题描述】:
我正在尝试从 ListView 中删除项目/行,但困难在于我还需要传递一些委托或触发一些事件或其他东西,所以当一个人单击按钮删除该行时,我的代码在其他地方处理一些其他逻辑(例如,从数据库中删除项目或其他)。
我制作了一个自定义控件:
public class SportsTeam : StackLayout { .. }
在此控件中,其中一个元素是 ListView,它列出了运动队中的所有人。
var viewModel = teamMembers.Select(x => new SportsTeamViewModel(x));
return new ListView
{
HasUnevenRows = true,
ItemSource = viewModel,
ItemTemplate = new DataTemplate(typeof(SportsTeamViewCell));
};
在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;
}
}
从这里开始,我不知道如何使按钮触发事件或某些删除可以通过构造函数传入,因此针对单个单元格/行/项目执行一些自定义逻辑被删除。
【问题讨论】:
-
将要删除的行的 ID 传递给 RemoveButton 的单击事件处理程序。
-
如何找到要删除的行的Id?或者每一行可以有另一个我给它的特殊 ID 吗?像一些隐藏的魔法ID?或者来自绑定数据的其他东西的价值?例如。
Id或UserName或其他...... -
是的,您必须使用 Bindings 并将模型的 Id 属性绑定到按钮的单击事件。
标签: c# listview xamarin xamarin.forms