Kao
Kao

Reputation: 106

DataGrid's values not updating when source is modified unless double clicked

My DataGrid's values does not seem to update when the values in it is modified.

I have tried:

  1. Setting the Mode to TwoWay
  2. Using BindableCollection (from Caliburn.Micro)
  3. Using ObservableCollection
  4. Using a List
  5. Merging QuotationViewModel with TableViewModel
  6. Adding an item in Work and removing it
  7. Using x:Name="Work" in the DataGrid instead of ItemsSource

TableViewModel.cs

public class TableViewModel : Screen
{
    [ ... ]
    public QuotationViewModel QuotationViewModel { get; set; }
    public void Update()
    {
        NotifyOfPropertyChange(() => WoodTotal);
        QuotationViewModel.Update();
    }
}

TableView.xaml

<!--Total-->
<ContentControl Grid.Column="1"
                cal:View.Model="{Binding Path=QuotationViewModel}"/>

QuotationViewModel.cs

public class QuotationViewModel : INotifyPropertyChanged
{
    private readonly TableViewModel _tableViewModel;

    public QuotationViewModel(TableViewModel tableViewModel)
    {
        _tableViewModel = tableViewModel;
    }

    public BindableCollection<ICanCalculate> Work { get; set; } = App.Config.Work;

    public void Update()
    {
        QuotationRow.CalculateQuotations(_tableViewModel.WoodTotal);
        OnPropertyChanged(nameof(Work));
    }
}

QuotationView.xaml

<DataGrid ItemsSource="{Binding Path=Work, Mode=TwoWay}" />

Repository: https://github.com/hizamakura/Furniture

As seen in this gif, the value updates when I double click it, it should update without having to double click it. I know that the bindings are correct because when I update Value, say of Milling to 0.20, the Total will change to 20% that of the Mahogany.

Upvotes: 0

Views: 214

Answers (1)

Kao
Kao

Reputation: 106

I just turned to creating a new BindableCollection whenever I want to update it.

public BindableCollection<ICanCalculate> Work { get; set; } = App.Config.Work;

public void Update()
{
    Work.CalculateQuotations(_tableViewModel.WoodTotal);
    Work = new BindableCollection<ICanCalculate>(Work);
}

Last time CalculateQuotations was just a method that operates on the same object, therefore the property isn't set again, only the properties inside is changed. i.e. the property is still the same object. Even if I call OnPropertyChanged(nameof(Work)) explicitly it didn't fix it, but this puts a new object and it updates everything.

Upvotes: 0

Related Questions