0

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

Robert
  • 2,407
  • 1
  • 24
  • 35
Lord Amateur
  • 19
  • 1
  • 2
  • 1
    As a note, setting `NotifyOnSourceUpdated=True`, `NotifyOnTargetUpdated=True` and `UpdateSourceTrigger=PropertyChanged` on the ItemSource Binding is pointless. The latter only has an effect in TwoWay or OneWayToSource Binding, while the other two are only needed if you attach handlers to the Binding's SourceUpdated and TargetUpdated events. – Clemens Feb 27 '20 at 09:19
  • 1
    Your property never fires the OnPropertyChanged. You have to implement the interface yourself or if you are using some other MVVM framework you usually do that by marking the property as virtual. – Robert Feb 27 '20 at 09:25
  • If you are using ObservableCollection, it will only raise property changed event when adding or removing item in the list. – Guillaume Ladeuille Feb 27 '20 at 09:27
  • @Clemens Thanks for pointing that out. I was desperately trying everything hoping to make it work. – Lord Amateur Feb 27 '20 at 10:05

0 Answers0