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!