0

I'm quite new to WPF so I'm bit struggling with refreshing values of some columns in my data grid when some other column changes its value.

XAML:

<DataGrid x:Name="dataGrid" HorizontalAlignment="Left" CanUserSortColumns="True" CanUserAddRows="False" CanUserReorderColumns="False" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Index" Binding="{Binding Path=Index}" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding Path=Description}" Header="Description" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding Path=Price}" Header="Price" Width="Auto" IsReadOnly="True"/>
<DataGridTextColumn x:Name="quantityBox" Binding="{Binding Path=Quantity, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="Quantity"/>
<DataGridTextColumn Binding="{Binding Path=Discount, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="False" Header="Discount(%)"/>
<DataGridTextColumn Binding="{Binding Path=TotalPrice, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="Total" IsReadOnly="True" Width="Auto"/>
</DataGrid.Columns>
</DataGrid>

This is class I use:

public class MyClass
{
    public int Index { get; set; }
    public string Description { get; set; }
    public double Price { get; set; }
    public int Quantity { get; set; }
    public double Discount { get; set; }
    public double TotalPrice { get; set; }
}

And I set List as ItemsSource for my dataGrid:

List<MyClass> items = GetFromDB();
dataGrid.ItemsSource = items;
dataGrid.Items.Refresh();

Now, what I want to do is to update/refresh "TotalPrice" once user changes "Discount" or "Quantity" column and reflect that change immediatelly in dataGrid.

After checking existing questions here on Stackoverflow and MSDN documentation I got this can be done via INotifyPropertyChanged event but I don't actually quite get how.

Can someone please elaborate in details how this is done or point out some detailed tutorial that addresses same/similar problem?

Thanks!

1 Answers1

1

You have to implement INotifyPropertyChanged in your ViewModel MyClass. When changing the Discount or Quantity property your TotalPrice should be calculated and when you have implemented INotifyPropertyChange the view will recognize the change and update. When you not implement this interface your View will get no notifications when your ViewModel is changed.

MyClass should look like this (only did it for one property as example)

public class MyClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

    double totalPrice;
    public double TotalPrice
    {
        get
        {
            return totalPrice;
        }

        set
        {
            totalPrice = value;
            OnPropertyChanged("TotalPrice");
        }
    }
}
Mighty Badaboom
  • 6,067
  • 5
  • 34
  • 51