DataGrid的计算列 [英] DataGrid calculated columns

查看:206
本文介绍了DataGrid的计算列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的Excel的应用程序转移到WPF DataGrid中。我打算将数据输入到列A和B列中我想提出的计算采取previus细胞和A柱的当前单元格,并添加列B previus细胞。
计算示例:B2 = B1 +(A2-A1)。什么是这样做的最佳方法?

I am trying to transfer my excel app to WPF datagrid. I am going to enter data to Column A and in column B I would like to make calculation taking previus cell and current cell of A column and add Column B previus cell. calculation example : B2 = B1 + (A2-A1). What is best approach of doing so?

推荐答案

就个人而言,我会通过创建一个代表这些记录并在这个类实现INotifyPropertyChanged的类开始。

Personally, I'd start by creating a class that represents the records and implement INotifyPropertyChanged on that 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));
        }
    }
}



然后,在你后面的代码在窗口上你显示数据网格,你要订阅的PropertyChanged列表中的每个对象。然后,你就不得不手动计算,只要这些属性更改的列值。 。伊克,我知道,但它会工作。

Then, in your code behind on the window you're showing the datagrid, you'll want to subscribe to PropertyChanged on each object in the list. Then you'd have to manually compute the column values whenever those properties changed. Ick, I know, but it'd work.

属性更改事件看起来像:

The property changed event would look like:

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静态的。你必须提供(可能通过你的窗口上的静态属性)从静态方法获得DataGrid的举行,虽然。所以:

If you hook this event onto all the records in your bound list, then it'll fire whenever any property is changed. In the class above, that'd apply to both A and B. You can filter which properties you're interested in monitoring through e.PropertyName (a simple string comparison) and guage the business logic accordingly. If you want to maintain encapsulation, or at least, put the business logic for the object on the object itself, this method could be a static one on the class recordObject. You'd have to provide for getting hold of the datagrid from that static method, though (likely through a static property on your window). So:

public static void recordObject_PropertyChanged(object sender, PropertyChangedEventArgs e)

和连接这样:

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

这篇关于DataGrid的计算列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆