简体   繁体   中英

change value of one column in datagrid by changing another

I can change the value of quanity column in data grid, but i cant seem to update the total price column of the same row using celleditending event this is my xaml

<DataGrid AutoGenerateColumns="False" EnableColumnVirtualization="False" EnableRowVirtualization="False" IsSynchronizedWithCurrentItem="True" SelectedItem="{Binding SelectedItem}" Grid.Row="3" Grid.ColumnSpan="2" Name="DataGrid1" ItemsSource="{Binding DataGridItemsSource, Mode=TwoWay, IsAsync=True}" Margin="10,10,10,10" PreviewKeyDown="DataGrid1_PreviewKeyDown" SelectionChanged="DataGrid1_SelectionChanged" CellEditEnding="DataGrid1_CellEditEnding" BeginningEdit="DataGrid1_BeginningEdit" >
    <DataGrid.Columns>
        <DataGridTextColumn Header="Item Name" IsReadOnly="True" Binding="{Binding Path=ItemName}" Width="*"></DataGridTextColumn>
        <DataGridTextColumn Header="Item Price" IsReadOnly="True"  Binding="{Binding Path=ItemPrice}" Width="*"></DataGridTextColumn>
        <DataGridTextColumn x:Name="QuantityColumn" Header="Quantity" IsReadOnly="False"  Binding="{Binding Path=Quantity, Mode=TwoWay}" Width="*"></DataGridTextColumn>
        <DataGridTextColumn  Header="Total Price" IsReadOnly="True" Binding="{Binding Path=TotalPrice, Mode=TwoWay}" Width="*"></DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

And this is my c# WPF for

private void DataGrid1_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    DataGrid dg = sender as DataGrid;
    AddItem row = (AddItem)dg.SelectedItems[0];
}

From this i can easily get the new value of quantity but cant seem to figure out how to update the totalprice column of my datagrid.itemsource of the same row. any help will be appreciated

public class AddItem : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            public string ItemName { get; set; }
            public float ItemPrice { get; set; }
            public string Price { get; set; }
            public int Quantity { get; set; }
            public decimal TotalPrice { get; set; }
            public int Size
            {
                get { return Quantity; }
                set
                {
                    Quantity = value;
                    if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Size"));
                }
            }
        }

This is for the Additem what am i missing

Assuming that TotalPrice = Quantity * ItemPrice , and is already a computed property of AddItem , you need to add a PropertyChanged handler to each item:

foreach (var item in DataGridItemsSource)
{
    item.PropertyChanged += item_PropertyChanged;
}

and add the handler:

private void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    switch (e.PropertyName)
    {
        case "Quantity":
        case "ItemPrice":
            PropertyChanged(this, "TotalPrice");
            break;
    }
}

This will tell the UI that the total price has changed and will update the grid accordingly.

An alternate solution would be to extend the AddItem class to send the PropertyChanged event out. This would send the event everywhere the class is used, not just on this particular view model.

Your view model must implement INotifyPropertyChanged for this to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM