Get ListView from Button Command xamarin.forms MVVM

怎甘沉沦 提交于 2020-05-09 07:04:24

问题


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:

 <ListView x:Name="MissingVolumeListView"  
                          ItemsSource="{Binding Volumes}"
                          SelectedItem ="{Binding Id}"
                             >
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <ViewCell>
                                        <Grid x:Name="Item">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="*"/>
                                                <ColumnDefinition Width="40"/>
                                            </Grid.ColumnDefinitions>
                                            <Label  Text="{Binding Name}" Style="{StaticResource UsualLabelTemplate}"/>
                                            <Button Grid.Column="1" x:Name="DeleteButton" Text ="-" BindingContext="{Binding Source={x:Reference MissingVolumeListView}, Path=BindingContext}"   Command="{Binding DeleteVolumeCommand}" CommandParameter="{Binding Source={x:Reference Item}, Path=BindingContext}"/>
                                        </Grid>
                                    </ViewCell>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>

Then I get the DeleteVolumeCommand in my ViewModel:

 private ICommand _deleteVolumeCommand;
    public ICommand DeleteVolumeCommand
    {
        get
        {
            {
                if (_deleteVolumeCommand == null)
                {
                    _deleteVolumeCommand = new Command((e) =>
                    {
                        var item = (e as Volume);
                        int index = MissingVolumeListView.Items.IndexOf(item);                       
                    });
                }

                return _deleteVolumeCommand;
            }

        }
    }

As you can see, what I want is to get selected item when I click a Button in my ListView. Then, I want to get my ListView to get index of the selected item to delete it from my ListView

Thank you for your help


回答1:


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.



来源:https://stackoverflow.com/questions/47889370/get-listview-from-button-command-xamarin-forms-mvvm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!