用于删除 Xamarin.Forms 中当前 ListView 行的按钮
Button to delete current ListView Row in Xamarin.Forms
我正在使用数据绑定从 SQLite 数据库中填充 ListView
。我希望用户能够按下按钮并删除当前行。
Page1.xaml:
<ListView x:Name="recentUserList" Margin="20,0" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<ListView.ItemTemplate>
<DataTemplate >
<ViewCell>
<Frame Padding="5" Margin="0,0,0,5">
<Grid HeightRequest="50" HorizontalOptions="FillAndExpand" VerticalOptions="Start">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Text="{Binding UserName}"/>
<Button Grid.Column="3" Text="Delete" Clicked="BtnDelete_Clicked" />
</Grid>
</Frame>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Page1.cs:
protected override void OnAppearing()
{
base.OnAppearing();
recentUserList.ItemsSource = App.Database.GetUserList();
}
private void BtnDelete_Clicked(object sender, EventArgs e)
{
var user = sender as User;
recentUserList.Remove(user);
}
数据库:
public List<User> GetUserList()
{
return database.Query<User>("SELECT * FROM User;");
}
User.cs:
public class User: INotifyPropertyChanged
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
private string userName;
public string UserName
{
get
{
return userName;
}
set
{
if (userName!= value)
{
userName = value;
OnPropertyChanged("UserName");
}
}
}
}
这会导致 Remove
in recentUserList.Remove(user);
in Page1.cs 的以下错误。
Error CS7036: There is no argument given that corresponds to the required formal parameter 'value' of 'CollectionExtensions.Remove<TKey, TValue>(IDictionary<TKey, TValue>, TKey, out TValue)'
我怎样才能做到可以点击按钮删除给定的 ListView
item/row?
sender
对象的类型为 Button
而不是 User
,尽管在本例中 BindingContext
属性 的类型为 User
:
private void BtnDelete_Clicked(object sender, EventArgs e)
{
var btn = sender as Button;
var user = btn.BindingContext as User;
//recentUserList.Remove(user);
}
另外,recentUserList
是一个 ui 元素 (ListView
),它没有 .Remove()
方法。您需要做的是将它 ItemsSource
绑定到您将调用 .Remove() 方法的 ObservableCollection
。
我强烈邀请您阅读 related documentation
首先使用一个ObservableCollection
ObservableCollection<User> users;
protected override void OnAppearing()
{
base.OnAppearing();
recentUserList.ItemsSource = users = new ObservableCollection<User>(App.Database.GetUserList());
}
然后从数据源中删除 user
,UI 将更新
private void BtnDelete_Clicked(object sender, EventArgs e)
{
var btn = sender as Button;
var user = btn.BindingContext as User;
users.Remove(user);
}
我正在使用数据绑定从 SQLite 数据库中填充 ListView
。我希望用户能够按下按钮并删除当前行。
Page1.xaml:
<ListView x:Name="recentUserList" Margin="20,0" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<ListView.ItemTemplate>
<DataTemplate >
<ViewCell>
<Frame Padding="5" Margin="0,0,0,5">
<Grid HeightRequest="50" HorizontalOptions="FillAndExpand" VerticalOptions="Start">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Text="{Binding UserName}"/>
<Button Grid.Column="3" Text="Delete" Clicked="BtnDelete_Clicked" />
</Grid>
</Frame>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Page1.cs:
protected override void OnAppearing()
{
base.OnAppearing();
recentUserList.ItemsSource = App.Database.GetUserList();
}
private void BtnDelete_Clicked(object sender, EventArgs e)
{
var user = sender as User;
recentUserList.Remove(user);
}
数据库:
public List<User> GetUserList()
{
return database.Query<User>("SELECT * FROM User;");
}
User.cs:
public class User: INotifyPropertyChanged
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
private string userName;
public string UserName
{
get
{
return userName;
}
set
{
if (userName!= value)
{
userName = value;
OnPropertyChanged("UserName");
}
}
}
}
这会导致 Remove
in recentUserList.Remove(user);
in Page1.cs 的以下错误。
Error CS7036: There is no argument given that corresponds to the required formal parameter 'value' of 'CollectionExtensions.Remove<TKey, TValue>(IDictionary<TKey, TValue>, TKey, out TValue)'
我怎样才能做到可以点击按钮删除给定的 ListView
item/row?
sender
对象的类型为 Button
而不是 User
,尽管在本例中 BindingContext
属性 的类型为 User
:
private void BtnDelete_Clicked(object sender, EventArgs e)
{
var btn = sender as Button;
var user = btn.BindingContext as User;
//recentUserList.Remove(user);
}
另外,recentUserList
是一个 ui 元素 (ListView
),它没有 .Remove()
方法。您需要做的是将它 ItemsSource
绑定到您将调用 .Remove() 方法的 ObservableCollection
。
我强烈邀请您阅读 related documentation
首先使用一个ObservableCollection
ObservableCollection<User> users;
protected override void OnAppearing()
{
base.OnAppearing();
recentUserList.ItemsSource = users = new ObservableCollection<User>(App.Database.GetUserList());
}
然后从数据源中删除 user
,UI 将更新
private void BtnDelete_Clicked(object sender, EventArgs e)
{
var btn = sender as Button;
var user = btn.BindingContext as User;
users.Remove(user);
}