将表绑定到子属性

时间:2012-01-28 13:22:34

标签: c#-4.0 data-binding properties

已经有几个答案已经存在,但我还没有找到任何结论。这是我想要做的事情的主旨:

EquityInstrument

public class EquityInstrument : INotifyPropertyChanged
{
    private string _Symbol;
    public string Symbol
    {
        get
        {
            return _Symbol;
        }
        set
        {
            _Symbol = value;
            OnPropertyChanged("Symbol");
        }
    }

    public EquityInstrument(string Symbol)
    {
        this.Symbol = Symbol;
    }


    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string FieldName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(FieldName);
        }
    }
}

OptionInstrument

public class OptionInstrument : INotifyPropertyChanged;
{
    public readonly EquityInstrument UnderlyingInstrument;

    private double _StrikePrice;
    public double StrikePrice
    {
        get
        {
            return _StrikePrice;
        }
        set
        {
            _StrikePrice = value;
            OnPropertyChanged("StrikePrice");
        }
    }

    private DateTime _Expiration;
    public DateTime Expiration;
    {
        get
        {
            return _Expiration;
        }
        set
        {
            _Expiration = value;
            OnPropertyChanged("Expiration");
        }
    }

    public OptionInstrument(string Symbol, double StrikePrice, DateTime Expiration)
    {
        this.Symbol = Symbol;
        this.StrikePrice = StrikePrice;
        this.Expiration = Expiration;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string FieldName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(FieldName);
        }
    }
}

此代码启动选项表...

GridControl OptionGrid = new GridControl();
BindingList<OptionInstrument> BoundList = new BindingList<OptionInstrument>();

public void InitializeDataTable()
{
    OptionGrid.DataSource = new BindingSource() { DataSource = BoundList };

    BandedGridColumn Column0 = new BandedGridColumn();
    Column0.FieldName = "Symbol";

    BandedGridColumn Column1 = new BandedGridColumn();
    Column1.FieldName = "StrikePrice";

    BandedGridColumn Column2 = new BandedGridColumn();
    Column2.FieldName = "Expiration";

    BandedGridView MainView = (BandedGridView)OptionGrid.MainView;

    MainView.Columns.Add(Column0);
    MainView.Columns.Add(Column1);
    MainView.Columns.Add(Column2);

    BoundList.Add(new OptionInstrument("DELL", 12.22, new DateTime(2012, 10, 12));
    BoundList.Add(new OptionInstrument("MSFT", 13.23, new DateTime(2012, 09, 16));
    BoundList.Add(new OptionInstrument("SPY", 12.23, new DateTime(2012, 07, 18));
}
你怎么看?有没有好的设计模式来实现这个目标?

0 个答案:

没有答案