Computed DataGrid Columns

I am trying to port an excel application to a WPat datagrid. I am going to enter data in column A and column B. I would like to make a calculation using the previus cell and the current cell of column A and add the cell of column B of the previous one. calculation example: B2 = B1 + (A2-A1). What is best suited for this?

+5
source share
2 answers

Personally, I would start by creating a class that represents entries and implements INotifyPropertyChanged in this class.

public class recordObject : INotifyPropertyChanged
{
    private int a;
    public int A 
    { 
        get
        {
            return a;
        }
        set
        {
            a = value;
            OnPropertyChanged("A");
        }
    }

    private int b;
    public int B
    { 
        get
        {
            return b;
        }
        set
        {
            b = value;
            OnPropertyChanged("B");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

datagrid, PropertyChanged . , . Ick, , .

, , :

void recordObject_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    var objectList = DataGrid.ItemsSource as List<recordObject>;
    var myRecord = sender as recordObject;
    if (objectList != null && myRecord != null)
    {
        int idx = objectList.IndexOf(myRecord);
        // Perform your calculations here using idx to access records before and after the current record
        // making sure to check for list boundaries for top and bottom.
        // Also note that this will likely kick off cascading event calls so make sure you're only changing
        // the previous or following record object.
    }
}

, . A, B. , e.PropertyName( ) -. , , - , recordObject. , (, ). :

public static void recordObject_PropertyChanged(object sender, PropertyChangedEventArgs e)

:

record.PropertyChanged += new PropertyChangedEventHandler(recordObject.recordObject_PropertyChanged);
+4

. :

class SomeData 
{
  int A { get; set; }
  int B { get; set; }
  int AminusB { get { return A - B; } }
}
+4

All Articles