且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从按钮命令 xamarin.forms MVVM 获取 ListView

更新时间:2023-02-05 22:42:00

首先改变你的删除按钮 XAML.

First of all change your delete button XAML.

<Button Grid.Column="1" 
        Text ="-" 
        Command="{Binding Path=BindingContext.DeleteVolumeCommand, Source={x:Reference MissingVolumeListView}}" 
        CommandParameter="{Binding .}" /> 

该命令需要通过列表视图名称将其绑定上下文更改为视图模型.

The command needs to have its binding context changed to the view model via the listview name.

但是,命令参数可以只传递当前绑定,即列表视图项.

The command parameter however can just pass the current binding which is the list view item.

在您的视图模型中,您不能通过名称引用任何控件.而是使用列表视图将其 ItemsSource 绑定到的列表 - Volumes.

In your view model you cannot reference any controls via name. Instead use the list that the list view has its ItemsSource bound to - Volumes.

您可以直接删除该项目.

You can remove the item directly.

_deleteVolumeCommand = new Command((e) =>
{
    var item = (e as Volume);
    Volumes.Remove(item);                       
});

如果 Volumes 不是 ObservableCollection,请记住还要调用 notify 属性已更改.

Remember to also call notify property changed if Volumes is not an ObservableCollection.