1

我遇到了一个非常奇怪的问题。

基本思想是我有一个类来保存从交易 api 收到的关于外汇价格的数据。每个属性都已使用 NotifyPropertyChanged 方法设置,如下所示。

class RealTimeBar
{
    public event PropertyChangedEventHandler PropertyChanged;

    private const double EPSILON = 0.0000001;

    private int _id;
    private string _symbol;
    private int _time;
    private float _open;
    private float _high;
    private float _low;
    private float _close;
    int _volume;

    public RealTimeBar(int id, string symbol)
    {
        _id = id;
        _symbol = symbol;
    }

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    public int Id
    {
        get
        {
            return _id;
        }
        set
        {
            _id = value;
        }
    }

    public string Symbol
    {
        get
        {
            return _symbol;
        }
        set
        {
            if (value != _symbol)
            {
                _symbol = value;
                NotifyPropertyChanged("Symbol");
            }
        }
    }

    public int Time
    {
        get
        {
            return _time;
        }
        set
        {
            if (value != _time)
            {
                _time = value;
                NotifyPropertyChanged("Time");
            }
        }
    }

    public float Open
    {
        get
        {
            return _open;
        }
        set
        {
            if (value != _open)
            {
                _open = value;
                NotifyPropertyChanged("Open");
            }
        }
    }

    public float High
    {
        get
        {
            return _high;
        }
        set
        {
            if (value != _high)
            {
                _high = value;
                NotifyPropertyChanged("High");
            }
        }
    }

    public float Low
    {
        get
        {
            return _low;
        }
        set
        {
            if (value != _low)
            {
                _low = value;
                NotifyPropertyChanged("Low");
            }
        }
    }

    public float Close
    {
        get
        {
            return _close;
        }
        set
        {
            if (value != _close)
            {
                _close = value;
                NotifyPropertyChanged("Close");
            }
        }
    }

    public int Volume
    {
        get
        {
            return _volume;
        }
        set
        {
            if (value != _volume)
            {
                _volume = value;
                NotifyPropertyChanged("Volume");
            }
        }
    }


}

它是一个很长的类,但结构简单,如您所见。现在我连接到向我触发事件的 api,并通过将 api 的值设置为我定义的类来处理它。

    BindingList<RealTimeBar> _realTimeBarList = new BindingList<RealTimeBar>();
    public Hashtable _iForexHashtable = new Hashtable();

    private void _UpdateForexQuote(int tickerId, int time, double open, double high, double         low,    double close, int volume,
                             double wap, int count)
    {
        ///MessageBox.Show(tickerId.ToString());
        ((RealTimeBar)_iForexHashtable[tickerId]).Open = (float)open;
        ((RealTimeBar)_iForexHashtable[tickerId]).High = (float)high;
        ((RealTimeBar)_iForexHashtable[tickerId]).Low = (float)low;
        ((RealTimeBar)_iForexHashtable[tickerId]).Close = (float)close;
        ((RealTimeBar)_iForexHashtable[tickerId]).Volume = volume;

    }

经过一些设置后,_UpdateForexQuote 方法会将传入的信息分发到 RealTimeBar 类的属性中。一切都很好。

当我启动程序时,它不会更新。我以为没有数据进来。但是当我随机单击gridcontrol的A1cell中的某个位置,然后单击另一个B1cell时,之前的A1cell会更新。然后,如果我单击 C1cell,则 B1cell 将更新。如果您不单击一个单元格,它将永远不会更新。我给你看图:

在此处输入图像描述

如您所见,单击前三行后,前三行显示延迟数据,由于我从未触摸第四行,因此显示为零。条件是我刚刚单击了第五行 Low 单元格,这就是 Low 不更新但其他单元格更新的原因。这很奇怪。我之前在 devexpress 11 和 vs 2010 下使用相同的代码。但是现在在 devexpress 12 和 vs 2012 下,我遇到了这个以前从未发生过的问题。

更新:

下面是我使用的方法 1. 定义 bindinglist 和一个 hashtable, 2. 先将对象放入 hashtable,然后将 hashtable 中的对象添加到 bindinglist 3. 将 bindinglist 绑定到 gridcontrol。

private void earningButtonItem_ItemClick(object sender, ItemClickEventArgs e)
    {
        _iTimer.AutoReset = false;
        _iTimer.Enabled = false;
        switchStockPool = "Earning Stock";
        disconnectButtonItem.PerformClick();
        connectButtonItem.PerformClick();
        _iheitanshaoEarningDBConnect = new DBConnect("heitanshaoearning");
        List<string>[] tempList;
        int tempHash;
        tempList = _iheitanshaoEarningDBConnect.SelectSymbolHighLow();
        _quoteEarningOnGridList.Clear();

        ///tempList[0].Count
        for (int i = 0; i < tempList[0].Count; i++)
        {
            tempHash = Convert.ToInt32(tempList[0][i].ToString().GetHashCode());
            _iStockEarningHistHashtable[tempHash] = new QuoteOnGridHist(tempList[0][i], (float)Convert.ToSingle(tempList[1][i]), (float)Convert.ToSingle(tempList[2][i]), (float)Convert.ToSingle(tempList[3][i]));
            _iStockEarningHashtable[tempHash] = new QuoteOnGrid(tempList[0][i], 0, 0);
            _quoteEarningOnGridList.Add((QuoteOnGrid)_iStockEarningHashtable[tempHash]);
            reqMktDataExStock(tempHash, tempList[0][i].ToString());
        }

        List<string>[] tempVolumeList;
        tempVolumeList = _iheitanshaoEarningDBConnect.SelectAverageVolume();
        for (int i = 0; i < tempList[0].Count; i++)
        {
            tempHash = Convert.ToInt32(tempVolumeList[0][i].ToString().GetHashCode());
            ((QuoteOnGrid)_iStockEarningHashtable[tempHash]).Average_Volume = ((float)Convert.ToSingle(tempVolumeList[1][i])) / volumeDenominator;
        }

        gridControl.DataSource = _quoteEarningOnGridList;
    }
    /////////////////////

现在当价格更新事件到来时,下面的方法将更新哈希表中的对象属性。由于我在对象中定义了 Notifypropertychanged,它应该更新 bingdinglist 和 gridcontrol 中的对象。

  private void _UpdateStockMarketQuote(int tikcerId, int field, double price, int canAutoExecute)
    {
        ////MessageBox.Show(tikcerId.ToString() + field.ToString() + price.ToString());
        if (switchStockPool == "Selected Stock")
        {
            if (field == 4)
            {
                ((QuoteOnGrid)_iStockHashtable[tikcerId]).Gap_From_High = ((float)price - ((QuoteOnGridHist)_iStockHistHashtable[tikcerId]).High) / ((QuoteOnGridHist)_iStockHistHashtable[tikcerId]).Close;
                ((QuoteOnGrid)_iStockHashtable[tikcerId]).Gap_From_Low = ((float)price - ((QuoteOnGridHist)_iStockHistHashtable[tikcerId]).Low) / ((QuoteOnGridHist)_iStockHistHashtable[tikcerId]).Close;
                ((QuoteOnGrid)_iStockHashtable[tikcerId]).Last_Price = (float)price;
            }
            //else if (field == 1)
            //{
            //    ((QuoteOnGrid)_iStockHashtable[tikcerId]).Gap_From_High = ((float)price - ((QuoteOnGridHist)_iStockHistHashtable[tikcerId]).High) / ((QuoteOnGridHist)_iStockHistHashtable[tikcerId]).Close;
            //    ((QuoteOnGrid)_iStockHashtable[tikcerId]).Gap_From_Low = ((float)price - ((QuoteOnGridHist)_iStockHistHashtable[tikcerId]).Low) / ((QuoteOnGridHist)_iStockHistHashtable[tikcerId]).Close;
            //}
        }
        else if (switchStockPool == "Earning Stock")
        {
            if (field == 4)
            {
                ((QuoteOnGrid)_iStockEarningHashtable[tikcerId]).Gap_From_High = ((float)price - ((QuoteOnGridHist)_iStockEarningHistHashtable[tikcerId]).High) / ((QuoteOnGridHist)_iStockEarningHistHashtable[tikcerId]).Close;
                ((QuoteOnGrid)_iStockEarningHashtable[tikcerId]).Gap_From_Low = ((float)price - ((QuoteOnGridHist)_iStockEarningHistHashtable[tikcerId]).Low) / ((QuoteOnGridHist)_iStockEarningHistHashtable[tikcerId]).Close;
                ((QuoteOnGrid)_iStockEarningHashtable[tikcerId]).Last_Price = (float)price;

            }
            //else if (field == 1)
            //{
            //    ((quoteongrid)_istockearninghashtable[tikcerid]).gap_from_high = ((float)price - ((quoteongridhist)_istockearninghisthashtable[tikcerid]).high) / ((quoteongridhist)_istockearninghisthashtable[tikcerid]).close;
            //    ((quoteongrid)_istockearninghashtable[tikcerid]).gap_from_low = ((float)price - ((quoteongridhist)_istockearninghisthashtable[tikcerid]).low) / ((quoteongridhist)_istockearninghisthashtable[tikcerid]).close;
            //}
        }

    }
4

1 回答 1

0

您不仅需要PropertyChanged在类中有事件,还需要实现INotifyPropertyChanged. 这就是网格知道类可以通知更改的方式。

于 2013-05-21T09:21:47.083 回答