I have a datagrid whose items source is a data object. Here is the datagrid:
<DataGrid Grid.Row="5" Grid.ColumnSpan="2" CanUserAddRows="False"
AutoGenerateColumns="False" Margin="10" x:Name="DataGridTransaction"
ItemsSource="{Binding SalesDataGridList,NotifyOnSourceUpdated=True,NotifyOnTargetUpdated=True,UpdateSourceTrigger=PropertyChanged}">
<DataGrid.Columns>
<DataGridTextColumn Header="BrandName" Binding="{Binding Brandname,Mode=OneWay}"/>
<DataGridTextColumn Header="Rate" Binding="{Binding Rate,Mode=OneWay}"/>
<DataGridTemplateColumn Header="Quantity" IsReadOnly="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Inputquantity,UpdateSourceTrigger=LostFocus,Mode=TwoWay}" Margin="0" LostFocus="TextBox_LostFocus" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Amount" Binding="{Binding Amount,UpdateSourceTrigger=LostFocus,Mode=TwoWay}" x:Name="AmountColumn"/>
</DataGrid.Columns>
</DataGrid>
Also the items source Object is :
class SalesDataGrid
{
string brandname;
int inputquantity;
int rate;
int amount;
public string Brandname { get => brandname; set => brandname = value; }
public int Rate { get => rate; set => rate = value; }
public int Inputquantity {
get => inputquantity;
set{
inputquantity = value;
inputquantity = quantity;
Amount = quantity * rate;
}
}
public int Amount { get => amount; set => amount=value; }
}
Here is what I am trying to do: You can see that, since the itemsource of the datagrid is the object list(a bindable collection),SalesDataGridList. The Quantity Column is a textbox column meant to take input from the user. Whenever an object is added in the list, the datagrid gets updated accordingly. Also I have put a simple logic in the object itself to set the value of the amount(=Quantity*Rate), so that whenever the user inputs a quantity value. The value of the amount variable does get updated in the object list, but the datagrid is unable to show the change of the value in the datagird.( If you double click the a cell in the amount column you can see the updated value but I want it to update automatically).
Note: The MVVM framework I am using, (caliburn.micro) provides a INotifyPropertyChanged() Function, But I can't implement that since the object itself has to inherit form the screen class,(and I dont think it is the way). I did try this method but it did not work