How to show a DataTable inside a contentPage

giulichajari

New member
Joined
Nov 6, 2024
Messages
3
Programming Experience
1-3
I Have a ContentPage for showing a product store list(amount, product, and delete option). Xaml looks like:

Picker and add button:
                     ....       
                                        <Button Text="Agregar"
                            Command="{Binding AgregarProductoCommand}" 
                             CommandParameter="{Binding Path=ProductoSeleccionado, Converter={StaticResource ProductoCantidadConverter}, ConverterParameter={Binding Path=Cantidad}}"
                            BackgroundColor="Beige"
                            Margin="0,10,0,10"/>
                                            <!-- Tabla para mostrar productos agregados -->
                                            <CollectionView ItemsSource="{Binding ProductosAgregados}">
                                                <CollectionView.ItemTemplate>
                                                    <DataTemplate>
                                                        <Grid Padding="5" ColumnDefinitions="*, Auto, Auto">
                                                            <!-- Columna para el nombre del producto -->
                                                            <Label Text="{Binding producto}" Grid.Column="0"
               FontAttributes="Bold"
               VerticalOptions="Center" />
                                                            <!-- Columna para la cantidad -->
                                                            <Label Text="{Binding Cantidad}" Grid.Column="1"
               HorizontalOptions="Center"
               VerticalOptions="Center" />
                                                            <!-- Botón para quitar el producto -->
                                                            <Button Text="-"
                Command="{Binding Path=BindingContext.QuitarProductoCommand, Source={x:Reference productosPicker}}"
                CommandParameter="{Binding .}"
                BackgroundColor="Red"
                TextColor="White"
                Grid.Column="2"/>
                                                        </Grid>
                                                    </DataTemplate>
                                                </CollectionView.ItemTemplate>
                                            </CollectionView>
C#:

After "agregar" (add button) click, i want adding the product to the observabeCollection to show the DataTemplate:



MvTareas bindingContext:
  public ICommand AgregarProductoCommand { get; }
  public ICommand QuitarProductoCommand { get; }
  public ObservableCollection<Producto> ProductosAgregados { get; set; }

 public MvTareas()
 {
     Productos = new ObservableCollection<Producto>();

     ProductosAgregados = new ObservableCollection<Producto>();

     AgregarProductoCommand = new Command<Tuple<Producto, decimal>>((parametros) =>
     {
         AgregarProducto(parametros.Item1, parametros.Item2);
     });
     QuitarProductoCommand = new Command<Producto>(QuitarProducto);
     .....
}

private void AgregarProducto(Producto producto, decimal cantidad)
{
    // Buscas si el producto ya está en la lista de productos agregados
    var existente = ProductosAgregados.FirstOrDefault(p => p.id == producto.id);
    if (existente != null)
    {
        existente.Cantidad++;  // O puedes incrementar por 'cantidad' si quieres sumar la cantidad
    }
    else
    {
        var nuevoProducto = new Producto
        {
            id = producto.id,
            producto = producto.producto,
            Cantidad = cantidad
        };
        ProductosAgregados.Add(nuevoProducto);
    }
}

As you can see above if the product was already sent i sum the amount or i just added to the list. Problem is that the add button doesnt do anymore when i click on it
 
Set a breakpoint at line 19 and then step through the code each time the button is pressed. Tell us what happens when the button is first pressed. Tell us what happens when the button is pressed a second time. Is this the expected behavior as you step through the code? Are you seeing the expected values as you step through the code? If you are not seeing the expected values and behavior tell us what you were expecting to see as compared to what you are actually seeing.
 
Back
Top Bottom