1

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
  • usage of x:Name is not MVVM pattern, try to get away from this. The delete command is on the viewmodels of your listitems, am i correct? When you create your list with the viewmodels, pass the list as a reference to the viewmodel. Then you can easily remove it in the items viewmodel. Commented Dec 19, 2017 at 17:39

1 Answer 1

15

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.