Get ListView from Button Command xamarin.forms MVVM

前端 未结 1 1768
借酒劲吻你
借酒劲吻你 2021-01-24 09:43

I have an issue on my ListView in my xamarin.forms application where I use MVVM pattern. I hope you could help me. Here is my xaml:

 

        
相关标签:
1条回答
  • 2021-01-24 10:31

    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.

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

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

    0 讨论(0)
提交回复
热议问题