Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

MVVM ListView MenuItem ObservableCollection Delete Item

$
0
0

Hi, I need to delete an item in my listview but it doesn't work
Here, xaml :

<ListView Grid.Row="4" Grid.ColumnSpan="3"
                ItemsSource="{Binding PaymentLines}"                               
                RowHeight="70" x:Name="items">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <ViewCell.ContextActions>
                                        <MenuItem  BindingContext="{Binding Source={x:Reference TicketRecapView}, Path=BindingContext}" Command="{Binding  DeleteCommand}" Text="Supprimer" IsDestructive="True" />
                                    </ViewCell.ContextActions>
                                    <Grid VerticalOptions="Center">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>                                       

                                        <Label Grid.Column="0"
                                       HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"
                                       FontSize="13"
                                       Text="{Binding Name}" />
                                        <Label Grid.Column="2"
                                       HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"
                                       FontSize="13"
                                       Text="{Binding Price}"  />
                                    </Grid>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>

Here my VM :
public PaymentConfirmationVM() {

    PaymentLines = new ObservableCollection<PaymentLigne>();

}

#region PaymentLines property
        ObservableCollection<PaymentLigne> _PaymentLines;

        public ObservableCollection<PaymentLigne> PaymentLines
        {
            get { return _PaymentLines; }
            set { SetProperty(ref _PaymentLines, value); }
        }
        #endregion

        #region Delete Command

        Command<PaymentLigne> _DeleteCommand;

        public Command<PaymentLigne> DeleteCommand
        {
            get { return GetProperty(ref _DeleteCommand, () => new Command<PaymentLigne>(Ligne => Delete(Ligne))); }
        }
        void Delete(PaymentLigne Ligne)
        {
            PaymentLines.Remove(Ligne);
        }

And here, my object observable :

public class PaymentLigne
    {
        public string Name { get; set; }
        public decimal Price { get; set; }

    }

I have an error when i put just a Command="{Binding DeleteCommand}" cause my object doesn't have the function DeleteCommand.
So i tried to put a reference for using the DeleteCommand in my VM. I don't know how to link, I think the problem is easy but I don't found a topic with MVVM.

Thx


Viewing all articles
Browse latest Browse all 89864

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>