avechuche
avechuche

Reputation: 1560

NumericUpDown in GridCell - WPF

I have a grid with a collection of "ItemPresupusto". I need to add a NumericUpDown (by mahApps) to be able to modify the "Cantidad" property of each "ItemPresupuesto" and every time I modify that property, I need to update data in the UI. I've tried everything, but I can not do it. Im use MVVM Light Some help. Thank you!

.XAML

<DataGrid IsReadOnly="True"
          SelectionUnit="FullRow"
          AutoGenerateColumns="False"
          GridLinesVisibility="Horizontal"
          ItemsSource="{Binding Articulos}">
            <DataGrid.Columns>
                    <DataGridTemplateColumn Header="Cantidad" MinWidth="100">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <mahApps:NumericUpDown Minimum="1"
                                IsTabStop="False"
                                Value="{Binding Cantidad, UpdateSourceTrigger=PropertyChanged}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
</DataGrid>

ViewModel

public class PresupuestosViewModel : ViewModelBase
{
     public IEnumerable<ItemPresupuesto> Articulos => new ObservableCollection<ItemPresupuesto>(Presupuesto.Items);
}

Class

public class ItemPresupuesto: EntidadBase

    {

        public decimal Cantidad { get; set; }

    }

public class Presupuesto : EntidadBase
{

    public virtual List<ItemPresupuesto> Items { get; }

}

Upvotes: 1

Views: 670

Answers (1)

mm8
mm8

Reputation: 169200

The ItemPresupuesto class should implement the INotifyPropertyChanged and interface and raise change notifications for the source property that is bound to the control that you want to refresh whenever the Cantidad or Prico properties are set:

public class ItemPresupuesto : INotifyPropertyChanged
{
    private decimal _cantidad;
    public decimal Cantidad
    {
        get { return _cantidad; }
        set { _cantidad = value; NotifyPropertyChanged(); NotifyPropertyChanged(nameof(Total)); }
    }

    private decimal _prico = 1;
    public decimal Prico
    {
        get { return _prico; }
        set { _prico = value; NotifyPropertyChanged(); NotifyPropertyChanged(nameof(Total)); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    public decimal Total => _prico * _cantidad;
}

Upvotes: 3

Related Questions